forked from xperzy/careerup150
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-1.cpp
57 lines (52 loc) · 1.34 KB
/
1-1.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;
class Solution{
public:
bool isunique1(string str){
for (int i=0;i<str.size();i++){
for (int j=0;j<str.size();j++){
if (i!=j && str[i]==str[j]){
return false;
}
}
}
return true;
}
bool isunique2(string str){
bool* A = new bool[256];
for (int i=0;i<str.size();i++){
if (A[str[i]]==true){
return false;
}else{
A[str[i]]=true;
}
}
return true;
}
/**
* Follow the idea on cc150, use one checker as bitmap to reduce the space complexity.
* Assumption: the string only has capital letter.
**/
bool isunique3(string str) {
int checker = 0;
for (int i = 0; i < str.size(); i++) {
int shift = str[i] - 'A';
if (checker & (1 << shift) > 0) return false;
checker |= 1 << shift;
}
return false;
}
};
int main(){
Solution sol;
string str = "asdafajkbczmvbus";
//cin >> str;
cout << str << ": ";
sol.isunique1(str) ? (cout << "true" << endl) : (cout << "false" << endl);
cout << str<< ": ";
sol.isunique2(str) ? (cout << "true" << endl) : (cout << "false" << endl);
cout << str<< ": ";
sol.isunique3(str) ? (cout << "true" << endl) : (cout << "false" << endl);
return 0;
}