Skip to content

Commit 153bf19

Browse files
feat: added AllUniques program in Strings
1 parent 6a481cb commit 153bf19

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

String/AllUniques.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)