Amazon Interview Question Partition Labels
The Question
A string
Solution
Let us break the question up to an objective parts
What is required is that no 2 partitions can have the same characters in them.
So what we are going to do is the following:
- Have a frequency table/array(called right) of the whole string.
- Iterate over string from the left and storing all character we have seen in a set and update the frequency table of the right.
- Whenever we have a character turn 0 in the table(the character is only in the left) we remove it from the seen set as it is only in the left now.
- If seen is empty that means that everything on the left is only in that partition hence we can add it to the ans array reset our count
Code In Java
Evaluation
Theoretical
Space Complexity: O(n)
Time Complexity: O(n)
Leetcode
Memory: 36.2MB
Runtime: 4ms