cswork / OAOpen practice workspace

AmazonOA #364

Erase Pairs

Estimated MediumJudge-ready

Topics (AI-labelled)Stacks & HeapsGreedyStrings

In each step you may delete any two equal characters, which need not be adjacent; the remaining characters keep their original order. First minimize the remaining length, then make the remaining string lexicographically smallest by Unicode code point.

Input

The input is a JSON string. On this site 0≤length in code points≤200000, and all Unicode scalar characters are allowed, including whitespace, NUL, non-BMP characters and U+0085/U+2028/U+2029.

Output

Output a canonical ASCII JSON string followed by a newline: double quotes and backslashes are escaped with a backslash; control characters use JSON short escapes (backspace, tab, newline, form feed, carriage return) or a lowercase four-digit \u; non-ASCII characters and DEL use a lowercase four-digit \u, and non-BMP characters use a pair of UTF-16 surrogate escapes; all other printable ASCII is output directly. This is equivalent to Python json.dumps(result, ensure_ascii=True). An empty string is output as "". The exact comparison mode is used, so spaces inside the string cannot be ignored.

Example 1

Input:

"CBCAAXA"

Output:

"BAX"

Example 2

Input:

"ZYXZYZY"

Output:

"XYZ"

Example 3

Input:

"ABCBACDDAA"

Output:

""

Example notes

The outputs are "BAX", "XYZ" and "". The original third example shows a single space, which conflicts with its explicit explanation of an empty string; this site corrects it to an empty JSON string.

Editorial

Loading solution…