Two Pass Through Approach:
1. iterate through the string, building a frequency map for the frequency of each character
2. iterate through the string, checking if the current character's frequency in the map is == 1, if it is , return i
- otherwise return -1
⌛ Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
💻 Implementation of Solution
class Solution {public: int firstUniqChar(string s) { unordered_map<char, int> freqMap; for(int i=0; i < s.size(); i++) { freqMap[s[i]]++; } for(int i=0; i < s.size(); i++) { if(freqMap[s[i]] == 1) return i; } return -1; }};