fork 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. // your code goes here
  13. }
  14. }
  15.  
  16. class Solution {
  17. public int firstUniqChar(String s) {
  18. HashMap<Character, Integer> count = new HashMap<Character, Integer>();
  19. int n = s.length();
  20. // build hash map : character and how often it appears
  21. for (int i = 0; i < n; i++) {
  22. char c = s.charAt(i);
  23. count.put(c, count.getOrDefault(c, 0) + 1);
  24. }
  25.  
  26. // find the index
  27. for (int i = 0; i < n; i++) {
  28. if (count.get(s.charAt(i)) == 1)
  29. return i;
  30. }
  31. return -1;
  32. }
  33. }
Success #stdin #stdout 0.07s 54636KB
stdin
Standard input is empty
stdout
Standard output is empty