A messy log-processing pipeline sometimes emits fields separated by a chosen delimiter character, but careless concatenation leaves runs of two or more consecutive delimiters, and sometimes a leading or trailing delimiter. Given a line of text S and a single delimiter character D, collapse every maximal run of consecutive D characters in S down to exactly one D, then remove any D that remains at the very start or the very end of the result. Every character of S other than D is left completely untouched (including if it is a space, and D may itself be a space).
Line 1: the text S (may be empty).
Line 2: a single character D.
The collapsed-and-trimmed string (may be empty).
S ≤ 200S and D contain only printable ASCII characters (0x20–0x7E)Example 1
Input
a,,b,,,c ,
Expected
a,b,c
Explanation
The delimiter is a comma; runs of commas collapse to one, giving `a,b,c`.
Example 2
Input
--start--middle-- -
Expected
start-middle
Explanation
The delimiter is `-`; every run collapses to one `-`, and the leading/trailing `-` are then trimmed, leaving `start-middle`.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →