Problem · String
Remove Excess Consecutive Characters
Learn this problemProblem 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) → StringExamples
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.Scontains only lowercase English letters.