Implement an algorithm to determine if a string has all unique characters.
You should decide if the string is an ASCII string or a Unicode string. . I will assume for simplicity the character set is ASCII. If this assumption is not valid, we would need to increase the storage size.
One solution is to create an array of boolean values, where the flag at index i indicates whether character i in the alphabet is contained in the string. The second time you see this character you can immediately return false.
but below is more simplistic approach..
public boolean uniqueChar(String str) {
HashSet<Character> unique = new HashSet<Character>();
for(int i=0; i<str.length();i++){
unique.add(str.charAt(i));
}
if(unique.size()!=str.length()){
return false;
}
return true;
}