vendredi 27 février 2015

determine if string has unique characters



The problem asks to "implement an algorithm to determine if a string has all unique character.


I saw the solution, but don't quite understand.



public boolean isUniqueChars(String str){
if(str.length()>256) return false;
boolean[] char_set = new boolean[256];
for(int i=0; i<str,length;i++){
int val=str.charAt(i);
if(char_set[val])
return false;
char_set[val]=true;
}
return true;
}


Do we not use parseIntor(int)converter in front of the code? (str.charAt[i] will be automatically change to int?) What does boolean[] char set=new boolean[256] mean? Why do we need to set char_set[val]=true?




Aucun commentaire:

Enregistrer un commentaire