ABC259C - XX to XXX

考え方

文字列を[(文字種1, 個数1), (文字種2, 個数2), ...]の形(ランレングス圧縮 ,連長圧縮)で表した上で,条件を考える.
Editorial - AtCoder Beginner Contest 259

回答例

S = input()
T = input()

def f(X):
    res = []
    cnt = 1
    for x1, x2 in zip(X, X[1:]):
        if x2 == x1:
            cnt += 1
        else:
            res.append((x1, cnt))
            cnt = 1
    res.append((x2, cnt))
    return res

S = f(S)
T = f(T)

if len(S) != len(T):
    exit(print('No'))

for s, t in zip(S, T):
    ch_s, cnt_s = s
    ch_t, cnt_t = t
    if ch_s != ch_t:
        exit(print('No'))
    if (cnt_s > cnt_t) or (cnt_s < cnt_t < 3) or (cnt_s < 2 <= cnt_t):
        exit(print('No'))

print('Yes')


前から順に圧縮しながら処理することもできる.

from collections import deque
S = deque(list(input()))
T = deque(list(input()))

while S and T:
    s = S.popleft()
    cnts = 1
    cntt = 0
    while S and s == S[0]:
        s = S.popleft()
        cnts += 1
    while T and s == T[0]:
        t = T.popleft()
        cntt += 1
    if cnts > cntt or (cnts < cntt < 3) or (cnts < 2 <= cntt):
        exit(print('No'))

if len(S) != len(T):
    exit(print('No'))

print('Yes')