Remove AB and CD Pairs
Updated
Problem statement
You are given a string s of uppercase English letters.
If "AB" or "CD" appears in s as two letters next to each other, delete those two letters. The letters on each side then become neighbors, which can make a new pair. Keep deleting until s contains neither "AB" nor "CD", and return the final string.
Only these two pairs are deleted, and their letters must be in this order. For example, "BA", "DC", "AD" and two equal letters all stay. The final string is the same no matter which pair you delete first. It can be the empty string.
Function
removePairs(s: String) → StringExamples
Example 1
s = "CABDAX"return = "AX"The only pair at the start is AB. Deleting it leaves "CDAX". Now C and D are next to each other, so delete CD to get "AX".
AX is not a pair, so the answer is "AX".
Example 2
s = "AACDBB"return = ""At first the only pair is CD in the middle. Deleting it leaves "AABB".
Now the middle letters form AB. Deleting them leaves "AB", and deleting that pair leaves the empty string.
Example 3
s = "BADCACBD"return = "BADCACBD"The neighbors are BA, AD, DC, CA, AC, CB and BD. None of them is AB or CD.
BA and DC have the right letters in the wrong order, and AD and CB mix the two pairs. Nothing is deleted, so the string stays the same.
Constraints
1 <= s.length <= 2 * 10^5scontains only uppercase English letters'A'to'Z'.
More Hudson River Trading Online Assessment questions
- Count Palindrome Addition ChoicesOA · Seen Nov 2026
- Count Multiples of Three with Two SevensOA · Seen Oct 2026
- Recursively Expanded Shell Command CountsOA · Seen Oct 2026
- Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
- Maximum Diamond SumOA · Seen Oct 2026
- Smallest Most-Lit Street PositionOA · Seen Oct 2026
- Rearrange String by Alternating EndsOA · Seen Sep 2026
- Most Items Within Budget from a Start IndexOA · Seen Sep 2026
- Count Sawtooth SubarraysOA · Seen Aug 2026
- Backtick Identifier ConverterOA · Seen Jun 2026
Similar questions at other companies
- Validate a Bracket SequenceInMobi · OAInMobi interview questions
- Valid ParenthesesBlackRock · OABlackRock interview questions
- Valid ParenthesesVanguard · OAVanguard interview questions
- Valid Bracket SequenceNVIDIA · OA · PHONE SCREENNVIDIA interview questions
- Get Final StringIBM · OAIBM interview questions
- Backspace String CompareGoldman Sachs · OAGoldman Sachs interview questions
- The PromHackerEarth · OA
- Compare Character Arrays with BackspacesOdoo · OAOdoo interview questions
- Eliminate SubstringJP Morgan Chase · OAJP Morgan Chase interview questions
- Balancing ParenthesesOracle · OAOracle interview questions