We need to find the first unique character in the given input string. For this, we iterate over the string and create a frequency map/array. I usually like to use a size 26 array, if I am given only lowercase or only uppercase string. A map is easier to go with if it’s a case-sensitive string.
We first create the frequency map. Then iterate over string again and check if the the current letter has a frequency of , if so return this index.
Note: in Python,
ord()
is used for getting the integer Unicode representation of a character. See docs.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity here is as there are no nested loops and we have visited the characters of source and target strings once in a loop.
-
Memory
The memory usage is since we have arrays of size 26: so, constant space!
— A