Java examples for Algorithm:String
is String having Unique Character Using Array, Time: O(n), Space: O(1)
public class Main { public static boolean isUnique(String str){ // Time: O(n), Space: O(1) if(str.length()<=1) return true; else if(str.length()>256) return false; Character[] characters=new Character[256]; int val; for(int i=0; i<str.length(); i++){ val=str.charAt(i); if(characters[val]==null) characters[val]=str.charAt(i); else return false; }/*ww w.ja v a2 s . c om*/ return true; } // Test public static void main(String[] args){ String str="Hello"; System.out.print(isUnique(str)+" "); String str_2=""; System.out.print(isUnique(str_2)+" "); String str_3="He"; System.out.print(isUnique(str_3)+" "); String str_4=" "; System.out.print(isUnique(str_4)+" "); } }