MicrosoftOA #3
Erase Pairs of Identical Letters
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsGreedyStrings
Each operation may delete any two equal letters, which do not need to be adjacent; the remaining characters keep their relative order. First minimize the length, then find the lexicographically smallest string among those of that length. Some of the original site's examples deleted the wrong characters; this site recomputed them under this explicit definition.
Input
One line of 1..100000 uppercase English letters.
Output
Output the final string; if it is empty, output an empty line.
Example 1
Input:
CBCAAXA
Output:
BAX
Example 2
Input:
ZYXZYZYV
Output:
XYZV
Example 3
Input:
ABCBACDDAB
Output:
AB
Example notes
Example 1: C has an even frequency and is deleted entirely, while B, A, and X are each kept once; the smallest result that preserves the order is BAX.
Example 2: Z, Y, X, and V all have odd frequencies, and the smallest feasible subsequence is XYZV.
Example 3: A and B each appear 3 times and C and D each appear 2 times; the shortest result is AB, not the empty string given by the original site.
Editorial
Loading solution…