公式

B - Commencement 解説 by en_translator


First, we count the occurrences of each character in \(S\). Prepare a length-\(26\) array \(\mathrm{cnt}\); for \(i=1,2,\dots,26\), let \(\mathrm{cnt}[i] =\) (the number of occurrences of the \(i\)-th character of the alphabet in \(S\)).

Next, for each integer \(i\) greater than or equal to \(1\), we inspect the number of occurrences of \(i\) in \(\mathrm{cnt}\). Prepare a length-\(100\) array \(\mathrm{cnt2}\); for \(i=1,2,\dots,100\), let \(\mathrm{cnt2}[i] =\) (the number of occurrences of \(i\) in \(\mathrm{cnt}\)).

After filling these arrays, \(\mathrm{cnt2}[i]\) equals the number of distinct characters that occur exactly \(i\) times in \(S\). Hence, one can answer the original problem by checking if all values of \(\mathrm{cnt2}\) are \(0\) or \(2\).

Sample code (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")

投稿日時:
最終更新: