first-unique-character.dart (642B)
1 //Find the first character in a string that is not recurring then return the index. 2 //The time complexity of this code is O(n) where n is the number of charcters in the string. 3 //Time: 297ms Beats: 83.58% 4 //Memory: 144.1MB Beats: 41.79% 5 6 class Solution { 7 int firstUniqChar(String s) { 8 Map<String, int> letters = {}; 9 for(int i = 0 ; i < s.length ; ++i){ 10 if(letters.containsKey(s[i])){ 11 letters[s[i]] = 2; 12 } 13 else{ 14 letters[s[i]] = 1; 15 } 16 } 17 for(int i = 0 ; i < s.length ; ++i){ 18 if(letters[s[i]] == 1){ 19 return i; 20 } 21 } 22 return -1; 23 } 24 }