The ability to debug and overall thinking need to improve
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode" return 0. s = "loveleetcode", return 2.
Note: You may assume the string contain only lowercase letters.
Solution: using linkedHashMap<> : have the insertion order!
class Solution { public int firstUniqChar(String s) { Map<Character, Integer> map = new LinkedHashMap<Character, Integer>(); for(int i = 0; i<s.length(); i++){ Character c = s.charAt(i); if(map.containsKey(c)){ map.put(c, 2); }else { map.put(c,1); } } char temp = '#'; for(Map.Entry<Character, Integer> entry : map.entrySet()){ System.out.println(entry.getKey() + " " + entry.getValue()); if(entry.getValue() == 1){ temp = entry.getKey(); break; } } for(int i = 0; i<s.length(); i++){ if(s.charAt(i) == temp) return i; } return -1; } }