If we want to partition, we must maintain Two Pointers at the beginning and the end of a section. The Next Partition will start at the end of last Partition.
While scanning the string, if all the characters in the Partition only appear in the Partition, we can Partition it.
As the picture below shows, the farthest position of all the chars is no more than \(8\), so we can partition it.
\(\texttt{FreqStack()}\) constructs an empty frequency stack.
void \(\texttt{push(int val)}\) pushes an integer val onto the top of the stack.
int \(\texttt{pop()}\) removes and returns the most frequent element in the stack.
If there is a tie for the most frequent element, the element closest to the stack’s top is removed and returned.
The requirements of the problems can be listed in 3 rules:
Pop the stack by the max frequency;
Among all the elements with the same (maximum) frequency, pop the element closest to the stack’s top.
To simplify the problem, let’s just ignore rule 2 for now, it looks easier.
Step 1
We care more about the frequency of the elements. We can use \(\texttt{HashMap}\) to record the frequency of each element.
Additionally, we also care about max Frequency, the current maximum frequency of any element in the stack. we use the \(\textit{maxFreq}\) to store the maximum frequency.
Given a string \(\textit{s}\), remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
The requirements of the problems can be listed in 3 rules:
Remove duplicate letters;
The order of characters in the deduplicated string cannot disrupt the relative order of the characters in s;
Among all deduplicated strings that meet the previous requirement, the one with the smallest lexicographical order is the final result.
Step 1
Let’s just ignore rule 3 for now, and use Stack to follow rule 1 and rule 2:
There are many approaches about this problem, like Stack, Divide and Conquer, Count Cores and so on. Here shows a Tricky and Clean solution.
Intuition
The sum will be a sum of powers of \(2\), as every core (a substring \(()\), with score \(1\) ) will have it’s score multiplied by \(2\) for each exterior set of parentheses that contains that core.
Since \(s\) is a balanced parentheses string, we can replace \(()\) by \(1\), then the result is still a balanced parentheses string.
For example, \((()())\) will become \((1(1))\). The sum is \((1 + 1 \times 2) \times 2 = 1 \times 2 + 1 \times 2 \times 2 = 6\).
As a result, let \(base = 1\), we can make \(base = base \times 2\) when we meet \((\), \(base = base \div 2\) when we meet \((\), and add \(1\) when we meet \(1\).
All elements must be pushed in in order. The key is how to pop them out?
Assuming that the value of the current top element of the stack is \(1\), and the next value to be popped in the corresponding popped sequence is also \(1\), then this value must be popped out immediately. Because the subsequent push will change the top element of the stack to a value other than \(2\), so the popped sequence of the popped numbers does not correspond.
Pushes each number in the pushed queue onto the stack, checking to see if the number is the next value to be popped in the popped sequence, and pops it if so.
Finally, check that not all the popped values are popped.