Given a list of strings find the first non repeating character, return index if true if not return −1
Solution
Non optimal brute force solution: using two pointers we can track of the current index and the index as we iterate throughout the entire string. If we find a duplicate then we move the current index one, if we iterated through the whole string with no duplicates found then we stop there and return the index of the non repeating character. This solutions run in O(n2) time and O(1) space. We are iterating through the string with two nested for loops thus causing this time complexity and we are not using any new space, just simply returning a value.
Using a hash map: Since we are dealing with input that is limited to lowercase english alphabet the possible unique values is limited to O(26). Therefore, the space complexity can be considered of O of constant value. However, if the input string was not limited and accepted any value as a character then this would not apply.
To implement this algorithm we would simply create a hash map that maps the characters and their frequency
Once we have this we can simply loop through the hash map and check for any key that is equal to one.
This is an O(1) space and O(n) time. We iterate twice which is O(n) + O(n) = O(2n) which is simply O(n).