leetcode-cn Daily Challenge on December 23th, 2020.
Difficulty : Easy
Related Topics : HashTable、String
Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1.
s = "leetcode" return 0. s = "loveleetcode" return 2.
- You may assume the string contains only lowercase English letters.
- mine
- Java
Runtime: 4 ms, faster than 98.88%, Memory Usage: 39.2 MB, less than 90.15% of Java online submissions
//O(N)time //O(1)space public int firstUniqChar(String s) { char[] arr = s.toCharArray(); int[] record = new int[26]; for(char c : arr){ record[c - 'a']++; } for(int i = 0; i < arr.length; i++){ if(record[arr[i] - 'a'] == 1) return i; } return -1; }
- Java