fork(5) 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 int firstUniqueChar(String str) {
  11.  
  12. int index = -1;
  13. str = str.toLowerCase();
  14.  
  15. Map<Character, Integer> charFreqMap = new HashMap<>();
  16.  
  17. // Update the map
  18. for (int i = 0; i < str.length(); i++) {
  19. char c = str.charAt(i);
  20.  
  21. // Get the current frequency
  22. int freq = charFreqMap.getOrDefault(c, 0);
  23.  
  24. // Update the map
  25. charFreqMap.put(c, (freq + 1));
  26. }
  27.  
  28. for (int i = 0; i < str.length(); i++) {
  29. if (charFreqMap.get(str.charAt(i)) == 1) {
  30. index = i;
  31. break;
  32. }
  33. }
  34.  
  35. return index;
  36. }
  37. public static void main (String[] args) throws java.lang.Exception
  38. {
  39. // your code goes here
  40. Ideone x = new Ideone();
  41. System.out.println(x.firstUniqueChar("lotsOfLove"));
  42. }
  43. }
Success #stdin #stdout 0.08s 32668KB
stdin
Standard input is empty
stdout
2