Unique Characters in a String
Java
code posted
by
Fayimora
created at 01 Feb 12:21
Edit
|
Back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
class C1_1 { public static void main(String[] args) { System.out.println(isUnique("fayimora")); System.out.println(isUnique("fayi")); System.out.println(isUnique("aaaaaaaa")); System.out.println(isUnique("")); System.out.println(isUnique(" ")); System.out.println("\n\nUNIQUED"); System.out.println(isUniqueD("fayimora")); System.out.println(isUniqueD("fayi")); System.out.println(isUniqueD("aaaaaaaa")); System.out.println(isUniqueD("")); System.out.println(isUniqueD(" ")); } public static boolean isUnique(String word) { int length = word.length(); if(length==0) return true; for(int i=0; i<length; i++) { char a = word.charAt(i); for(int j=i+1; j<length; j++) { char b = word.charAt(j); if(a==b) return false; } } return true; } public static boolean isUniqueD(String word) { boolean[] ascii = new boolean[256]; for(int i=0; i<word.length(); i++) { int val = word.charAt(i); if(ascii[val]) return false; ascii[val]=true; } return true; } } |
1.06 KB in 3 ms with coderay