• Source
    1. import java.util.*;
    2.  
    3. class Main
    4. {
    5. public static boolean hasUniqueChars(String toTest)
    6. {
    7. char charArray[] = toTest.toCharArray();
    8. Arrays.sort(charArray);
    9.  
    10. for (int x = 1; x < charArray.length; x++)
    11. {
    12. if ( charArray[x] == charArray[x-1] )
    13. return false;
    14. }
    15.  
    16. return true;
    17. }
    18.  
    19.  
    20. public static void main(String args[])
    21. {
    22. String testData[] = {
    23. "abc",
    24. "abcc",
    25. "abcdefhi",
    26. "abcdefhiabcdefhi",
    27. "ccc",
    28. "xxxkjsdfkjqqqdkjfd",
    29. "abcdefghijklmnopqrstuvwxyz",
    30. };
    31.  
    32. for (String test : testData)
    33. {
    34. if ( hasUniqueChars(test) )
    35. {
    36. System.out.println(" hasUniqueChars(\"" + test + "\")");
    37. }
    38. else
    39. {
    40. System.out.println("! hasUniqueChars(\"" + test + "\")");
    41. }
    42. }
    43. }
    44. }
    45.  
    46. // vim: sw=4:ts=4:sts=4:et:ai: