PythonでJOI難易度5を埋める #31

A - JJOOII (JJOOII)

連続するJ,O,I を数える.JJOOOIIIのようなケースに気をつける.

s = input()

cnt_j, cnt_o, cnt_i = 0, 0, 0
ans = [0]
for i in range(len(s)):
    if cnt_i != 0 and s[i] != 'I':
        cnt_j, cnt_o, cnt_i = 0, 0, 0
    if s[i] == 'J':
        if cnt_o != 0 or cnt_i != 0:
            cnt_j, cnt_o, cnt_i = 0, 0, 0
        cnt_j += 1
    elif cnt_j != 0 and s[i] == 'O':
        cnt_o += 1
    elif cnt_j != 0 and s[i] == 'I':
        cnt_i += 1

    if cnt_j >= cnt_o and cnt_i == cnt_o and cnt_o > 0:
        ans.append(cnt_o)
        cnt_j, cnt_o, cnt_i = 0, 0, 0

print(max(ans))