fork download
  1. class FirstUniqueCharFinder {
  2. final static int ASCII_OFFSET = 97;
  3. final static int ALPHABET_LEN = 26;
  4.  
  5. static char find(String word) {
  6. int[] alphabet = new int[ALPHABET_LEN];
  7. char[] charArray = word.toLowerCase().toCharArray();
  8.  
  9. for (int codePoint: charArray) {
  10. int index = codePoint - ASCII_OFFSET;
  11. alphabet[index]++;
  12. }
  13.  
  14. char firstUniqueChar = 0;
  15.  
  16. for (int i = 0; i < word.length(); i++) {
  17. int index = charArray[i] - ASCII_OFFSET;
  18. if (alphabet[index] == 1) {
  19. firstUniqueChar = word.charAt(i);
  20. break;
  21. }
  22. }
  23.  
  24. return firstUniqueChar;
  25. }
  26.  
  27. public static void main(String[] args) {
  28. System.out.println(find("transaction"));
  29. System.out.println(find("reverse"));
  30. System.out.println(find("xyuxyuA"));
  31. }
  32. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
r
v
A