Last active
November 26, 2016 04:44
-
-
Save Sara3/eb24bace4bba2177d737ee1f48e68196 to your computer and use it in GitHub Desktop.
bitwise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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