File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ // An algorithm to check if a string has all unique characters.
2
+ // Sample Input-1: Hello
3
+ // Sample Output-2: All Characters are not Unique
4
+
5
+ // Sample Input-1: World
6
+ // Sample Output-2: All Characters are Unique
7
+
8
+
9
+ #include < bits/stdc++.h>
10
+ using namespace std ;
11
+
12
+ // time complexity = O(n)
13
+ // 128-character alphabet set
14
+ bool isAllUnique (string st)
15
+ {
16
+ if (st.length ()>128 )
17
+ return false ;
18
+
19
+ // initialize vector to zero flag
20
+ vector<bool > char_set (128 ,0 );
21
+ int val;
22
+ for (int i = 0 ; i < st.length (); i++)
23
+ {
24
+ val = st.at (i);
25
+ if (char_set[val])
26
+ {
27
+ // already found this character
28
+ return false ;
29
+ }
30
+
31
+ char_set[val] = true ;
32
+ }
33
+ return true ;
34
+ }
35
+
36
+ int main ()
37
+ {
38
+ string st;
39
+ getline (cin,st);
40
+ if (isAllUnique (st)){
41
+ cout<<" All Characters are Unique" <<endl;
42
+ }else {
43
+ cout<<" All Characters are not Unique" <<endl;
44
+ }
45
+ return 0 ;
46
+ }
You can’t perform that action at this time.
0 commit comments