No.345 最小チワワ問題

一回の走査で済む。

#!/usr/bin/env python3
s = input()
c, cw = None, None
ans = float('inf')
for i in range(len(s)):
    if s[i] == 'c':
        c = i
    elif s[i] == 'w':
        if cw is not None:
            ans = min(ans, i - cw + 1)
        if c is not None:
            cw = c
print([ans, -1][ans == float('inf')])