Official

C - String Delimiter Editorial by en_translator


Scan the characters from left to right, while maintaining whether the current one is enclosed. If it is enclosed and equals ,, replace it with ..

How can we determine if a character is enclosed? A character is enclosed if and only if there is an odd number of left to the character. Using this criteria, one can answer the problem in a linear time. See also the sample code.

Sample code (Python):

n = int(input())
s = list(input())
lef_even = True
for i in range(n):
    if lef_even and s[i] == ",":
        s[i] = "."
    if s[i] == '"':
        lef_even = not lef_even
print("".join(s))

posted:
last update: