Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Use
/*
- Function hasUniqueChars2
- Args - std::string
- Output:: True if string has all characters which are unique
-
False if string has at least one repeated char.
- Assumption:: string only contains (a..z), 26 chars. */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<string>
using namespace std;
bool hasUniqueChars2( std::string str)
{
int check = 0;
int len = str.length();
for (int i = 0; i < len; ++i)
{
int c = (int)(str[i] - 'a');
cout << c << endl;
if ( check & ( 1 << c ) ) {
return false;
}
check |= ( 1 << c);
}
return true;
}
int main()
{
std::string in="abbc";
cout << hasUniqueChars2(in) << endl;
}
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content