Official

B - Commencement Editorial by sotanishy


まず,それぞれの文字が \(S\) 中に現れる回数を調べます. 長さ \(26\) の配列 \(\mathrm{cnt}\) を用意し, \(i=1,2,\dots,26\) について, \(\mathrm{cnt}[i] =\) (アルファベットで \(i\) 番目の文字が \(S\) 中に現れる回数) とします.

次に,\(1\) 以上の整数 \(i\) について, \(i\)\(\mathrm{cnt}\) に現れる回数を調べます. 長さ \(100\) の配列 \(\mathrm{cnt2}\) を用意し, \(i=1,2,\dots,100\) について, \(\mathrm{cnt2}[i] =\) (\(i\)\(\mathrm{cnt}\) 中に現れる回数) とします.

これらの処理のあと, \(\mathrm{cnt2}[i]\) は, \(S\) にちょうど \(i\) 回現れる文字の種類数に一致します. よって, \(\mathrm{cnt2}\) の値がすべて \(0\) または \(2\) であるか調べることで,問題に答えることができます.

実装例 (Python)

S = input()
cnt = [0] * 26
for c in S:
    cnt[ord(c) - ord("a")] += 1
cnt2 = [0] * 101
for c in cnt:
    if c > 0:
        cnt2[c] += 1
print("Yes" if all(c in (0, 2) for c in cnt2) else "No")

posted:
last update: