Wednesday, September 3, 2014

Check if char in a string are unique


public class StringUniqueChar {
final static int TotalChar = 128; //Total Number of ASCII
public boolean isUnique(String s) {
if(s.length()>TotalChar) return false;
else {
boolean[] hash = new boolean[TotalChar];
for(int i=0; i<s.length(); i++) {
int possition = s.charAt(i);
if(hash[possition]==true) return false;
else hash[possition] = true;
}
return true;
}
}
}

UA-39217154-2