[DAY19]Reverse Shuffle Merge

Sherlock Chiou
1 min readOct 22, 2019

--

Solution: Because the permutation may have any order, so we need to find the reverse(A) with lexicographically largest order. Hence, when we reverse the whole string s, the target we are going to find would be A with lexicographically smallest order. First, we need to create three hash tables for recording the frequency number of each character, the number we have used of each character, and the remained amount of each character when we iterate through the string. Because the string s contains reverse(A) and shuffle(A). We can use at most char_frequency[char]//2 of the character. Another constraint would be if we don’t have enough amount number of character in the used hash table and the remained hash table. Then we must keep the character in the res list. When we iterate through the string s, we must check that if the character meet the two constraints mentioned above. Because we want the A to be lexicographically smallest, so a stack is used to make sure we remove element from the largest lexicographic character.

--

--

Sherlock Chiou
Sherlock Chiou

Written by Sherlock Chiou

Be Effective and Optimize for Learning

No responses yet