#C9739. Consecutive Character Removal
Consecutive Character Removal
Consecutive Character Removal
You are given a string P consisting of lowercase letters. Your task is to remove adjacent consecutive characters in the string repeatedly until no such adjacent equal characters remain.
More formally, let P be a string. If two adjacent characters are identical, they are both removed, and this process is repeated as long as possible. If after all removals the string is empty, output Empty
.
Examples:
- For
P = "abbaca"
, the final string isca
. - For
P = "azxxzy"
, the final string isay
.
Note: The removal process is performed sequentially from left to right; however, the order in which removals occur does not affect the final answer.
The removal process can be mathematically described as follows: $$ \text{Let } S = \text{an initially empty stack. For each character } c \text{ in } P:\ \quad \text{if } S \neq \emptyset \text{ and the top element of } S \text{ equals } c, \text{ then pop } S, \text{ else push } c.\ \text{Let the final string be the concatenation of elements in } S. \text{ If } S \text{ is empty, output "Empty".} $$
inputFormat
The input consists of a single line containing the string P (1 ≤ |P| ≤ 105), which comprises only lowercase letters.
outputFormat
Output the final string obtained after repeatedly removing adjacent identical characters. If the final string is empty, output Empty
.
abbaca
ca
</p>