Determine if a string has all Unique Characters | C ++ program to illustrate String with unique characters using set data structure ; Inserting character of string into set ; If length of set is equal to len of string then it will have unique characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool uniqueCharacters ( string str ) { set < char > char_set ; for ( char c : str ) { char_set . insert ( c ) ; } if ( char_set . size ( ) == str . size ( ) ) { return true ; } else { return false ; } } int main ( ) { string str = " GeeksforGeeks " ; if ( uniqueCharacters ( str ) ) { cout << " The ▁ String ▁ " << str << " ▁ has ▁ all ▁ unique ▁ characters STRNEWLINE " ; } else { cout << " The ▁ String ▁ " << str << " ▁ has ▁ duplicate ▁ characters STRNEWLINE " ; } return 0 ; }