Skip to content

Instantly share code, notes, and snippets.

@Sara3
Last active November 26, 2016 04:44
Show Gist options
  • Save Sara3/eb24bace4bba2177d737ee1f48e68196 to your computer and use it in GitHub Desktop.
Save Sara3/eb24bace4bba2177d737ee1f48e68196 to your computer and use it in GitHub Desktop.
bitwise
//is Unique: Implement an algorithm to determine if a //string has all unique characters. No additional
//data tractures
#include <iostream>
using namespace std;
//bruth force
bool isunique(string str){
int checker=0;
for(int i=0; i<str.size(); i++){
int val=str[i]-'a';
cout<<"Val = "<<val<<endl;
cout<<"1<<val = "<<(1<<val)<<endl;
if((checker&(1<<val))>0){ //shift 0 by 1, why is it one instead of 0?
return false;
}
checker=checker|(1<<val); //shouldn't this and the "(1<<val)" above be same? it is not
//also how a bool is assigned bigger number and what does that mean
cout<<"checker = "<<checker;
cout<<endl<<endl;
}
return true;
}
int main(){
string str;
str="abcde";
if(isunique(str))
cout<<"yes";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment