How to check if a string contains only unique characters in C++

1 Answer

0 votes
#include <bits/stdc++.h> 
 
using namespace std;
   
bool contains_unique_chars(string s) { 
    sort(s.begin(), s.end()); 
    
    int len = s.length();
    for (int i = 0; i < len; i++) { 
        if (s[i] == s[i + 1]) { 
            return false; 
        } 
    } 
    return true; 
} 
   
int main() 
{ 
   
    string s = "abcde"; 
   
    if (contains_unique_chars(s)) { 
        cout << "yes";
    } 
    else { 
        cout << "no";
    } 
    return 0; 
} 
 
 
 
/*
run:
 
yes
 
*/

 



answered Dec 28, 2019 by avibootz
edited Dec 31, 2019 by avibootz

Related questions

2 answers 277 views
1 answer 176 views
2 answers 281 views
1 answer 169 views
2 answers 206 views
4 answers 288 views
...