Problem · Stack

Zolando Manipulation

Learn this problem
MediumZolandoOA

Problem statement

A string S consisting of the letters A, B, C and D is given. The string can be transformed either by removing a letter A together with an adjacent letter B, or by removing a letter C together with an adjacent letter D.

Write a function:

class Solution { public String solution(String S); }

that, given a string S consisting of N characters, returns any string that:

  • can be obtained from S by repeatedly applying the described transformation, and
  • cannot be further transformed.
If at some point there is more than one possible way to transform the string, any of the valid transformations may be chosen.

Write an efficient algorithm for the following assumptions:

  • the length of string S is within the range [0..250,000];
  • string S is made only of the following characters: 'A', 'B', 'C' and/or 'D'.

Function

solution(S: String) → String

Examples

Example 1

S = "CBACD"return = "C"
One of the possible sequences of operations is as follows: - CBACD → CCD → C

Example 2

S = "CABABD"return = ""
One possible sequence of operations is: - CABABD → CDD → ""

Example 3

S = "ACBDACBD"return = "ACBDACBD"
No operation can be applied to string S, so the function should return "ACBDACBD".

More Zolando problems

drafts saved locally
public String solution(String S) {
  // write your code here 
    
}
S"CBACD"
expected"C"
checking account