Adjacent Pair and Run Removal
Problem statement
Given a string s, compute two independent results, each starting from the original string.
- Pair removal: repeatedly remove the leftmost pair of equal adjacent characters until no equal adjacent pair remains. Remove exactly two characters each time.
- Whole-run removal: in one round, identify every maximal contiguous run of equal characters whose length is at least two, and remove all such runs simultaneously. Newly adjacent characters are considered only in the next round. Repeat until a round removes nothing.
Return a two-element string array [pairResult, runResult] in that order. A maximal run includes every consecutive occurrence of the same character at that location. Characters separated by a different surviving character are not duplicates to remove.
The source reports an adjacent-duplicate task followed by a version without a supplied k. For this practice exercise, the follow-up uses the explicit simultaneous whole-run rule above; its round timing was not specified in the report.
Function
removeAdjacentVariants(s: String) → String[]Examples
Example 1
s = "abbbaa"return = ["ab","a"]Pairs: abbbaa → abaa → ab. Runs: remove bbb and the final aa together, leaving a.
Example 2
s = "abccba"return = ["",""]Pairs cancel cc, then bb, then aa. Runs remove cc in round 1, bb in round 2, and aa in round 3; both results are empty.
Example 3
s = "abcab"return = ["abcab","abcab"]No equal adjacent characters exist, so both results remain abcab.
Constraints
0 <= s.length <= 2000.scontains only lowercase English letters.- Both results start independently from the original
s; an empty input returns two empty strings.