Problem · String

Remove Excess Consecutive Characters

Learn this problem
EasyDRW logoDRWFULLTIMEOA

Problem statement

Given a lowercase string S, delete the minimum possible number of characters so that the result contains no three identical consecutive characters.

Return the resulting string. Characters that remain must keep their original relative order.

Function

solution(S: String) → String

Examples

Example 1

S = "eedaaad"return = "eedaad"

Deleting one a leaves no run longer than two.

Example 2

S = "xxxtxxx"return = "xxtxx"

Each run of three x characters loses one character.

Example 3

S = "uuuuxaaaaxuuu"return = "uuxaaxuu"

Each maximal run is shortened independently to at most two characters.

Constraints

  • S.length <= 200000.
  • S contains only lowercase English letters.

More DRW problems

drafts saved locally
public String solution(String S) {
    // Write your code here.
}
S"eedaaad"
expected"eedaad"
checking account