fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String s1 = "transaction";
  13. System.out.println(getFirstUniqueChar(s1));
  14.  
  15. String s2 = "reverse";
  16. System.out.println(getFirstUniqueChar(s2));
  17. }
  18.  
  19. public static char getFirstUniqueChar(String s) {
  20. int[] temp = new int[26];
  21. int alphabetNum;
  22. for (int i = 0; i < s.length(); i++) {
  23. alphabetNum = getAlphabetNum(s.charAt(i));
  24. if (temp[alphabetNum] == 0) {
  25. temp[alphabetNum] = i;
  26. } else {
  27. temp[alphabetNum] = -1;
  28. }
  29. }
  30.  
  31. for (int i = 0; i < s.length(); i++) {
  32. alphabetNum = getAlphabetNum(s.charAt(i));
  33. if (temp[alphabetNum] == i) {
  34. return s.charAt(i);
  35. }
  36. }
  37. return ' ';
  38. }
  39.  
  40. public static final int getAlphabetNum(char c) {
  41. c = Character.toLowerCase(c);
  42. return c - 'a';
  43. }
  44. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
r
v