I'm trying to find whether a particular string has all unique characters in it or not. My approach is like this, I'm initializing a variable of 64 bit, say places and setting it to 0. Now, I'm iterating over the string and calculating the difference between of ASCII between current character and 'A'(smallest ASCII possible). If (places & (1"<<"pos)) is already set, the string does not have unique characters.
Everything works fine but with only lowercase characters. The moment I add test with uppercase, the code doesn't work anymore. I'm sure its something with my variable places but I don't know what exactly is wrong.
Here is the code for the same :
#include <bits/stdc++.h>
using namespace std;
void check_unique(string s){
int64_t places=0;
for(int i=0;i<s.length();i++){
int pos=s[i]-'A';
if((places & (1<<pos))!=0){
cout<<"String does not have all unique characters\n";
return;
}
places|=(1<<pos);
}
cout<<"String has all unique characters\n";
}
int main() {
check_unique("abcde"); // Testcase 1
check_unique("aabb"); // Testcase 2
check_unique("ABbde"); // Testcase 3, Wrong output.
return 0;
}