public class Solution {
public static boolean isUnique(String str){
if(str.length()>256)
return false;
boolean[] visited = new boolean[256];
for(int i=0;i<str.length();i++){
int val = str.charAt(i);
if(visited[val])
return false;
visited[val] = true;
}
return true;
}
public static void main(String[] args) {
String[] words = {"abcde", "hello", "apple", "kite", "padle"};
for (String word : words) {
System.out.println(word + ": " + isUnique(word));
}
}
}
Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
Ask: Could you please tell me how large the character set is? Is it only 26 letters? Or is it ASCII? Or much more?