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. Scanner sc = new Scanner(System.in);
  14. String s = sc.next();
  15. int index = -1;
  16. Map<Character,Integer> map = new HashMap<>();
  17. for(int i=0;i<s.length();i++){
  18. map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);
  19. }
  20.  
  21. for(int i=0;i<s.length();i++){
  22. if(map.get(s.charAt(i))==1){
  23. index=i;
  24. break;
  25. }
  26. }
  27. System.out.println(index);
  28.  
  29. }
  30. }
Success #stdin #stdout 0.11s 56680KB
stdin
loveleetcode
stdout
2