Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.2 kB
3
Indexable
Never
k = int(input())
w = input()
a = []
b = []
s = []
d = []
f = []

for i in w:
    if i.isupper():
        a += [1]
    else:
        a += [2]

for _ in range(k):
    b += [1]
    s += [2]

r = 0
# 把<改成<=,這樣才能包含到最後一個字母
while(r+k <= len(a)):
    if a[r:(r+k)] != b and a[r:(r+k)] != s:
        r += 1
    else:
        d += [r]
        r += 1
    
if d == []:
        print(0)
else:
    for v in d:     
        
        counter = 0
        if a[v:(v+k)] == b:
            before = 1
            counter += k
        elif a[v:(v+k)] == s:
            before = 2
            counter += k
        for i in range(v+k, len(a)-k, k):
            if a[i:(i+k)] == b and before == 2:
                counter += k
                before = 1
            elif a[i:(i+k)] == s and before == 1:
                counter += k
                before = 2
            else:
                f += [counter]
                break
        # 如果最長子字串有包含最後的字母,就進不到上面的else
        # 因此,離開上面的for迴圈,還要再檢查一次counter!
        if counter != 0:    f += [counter]
    print(max(f))