summaryrefslogtreecommitdiffstats
path: root/Main/Python/2020/S3.py
blob: 482f50fee3b6ec7b4b40ce2440d035919faac971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
N = input()
H = input()

output = 0
seen = set()

letter_count = {}
for i in H[:len(N)]:
    letter_count[i] = letter_count.get(i, 0) + 1

needle_count = {}
for i in N:
    needle_count[i] = needle_count.get(i, 0) + 1

skip = 0

for i in range(len(N)-1, len(H)):
    if i != len(N) - 1:
        start = H[i - len(N)]
        letter_count[start] -= 1
        if letter_count[start] == 0:
            del letter_count[start]
        letter_count[H[i]] = letter_count.get(H[i], 0) + 1
    if skip > 0:
        skip -= 1
        continue
    if H[i] not in needle_count:
        skip = len(N) - 1
        continue
    buffer = H[i - len(N) + 1:i + 1]
    if buffer not in seen and letter_count == needle_count:
        output += 1
        seen.add(buffer)

print(output)