We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Problems like these are solved using a "Monotonic Stack".
The idea is to iterate through the string backwards and for keep popping off characters from the stack (as long as there are enough remaining characters of that type in the string) that have a value that is greater than that of the current character then pushing the current character onto the stack.
Reverse Shuffle Merge
You are viewing a single comment's thread. Return to all comments →
Problems like these are solved using a "Monotonic Stack". The idea is to iterate through the string backwards and for keep popping off characters from the stack (as long as there are enough remaining characters of that type in the string) that have a value that is greater than that of the current character then pushing the current character onto the stack.