-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2955-number-of-same-end-substrings.py
More file actions
34 lines (29 loc) · 1.06 KB
/
2955-number-of-same-end-substrings.py
File metadata and controls
34 lines (29 loc) · 1.06 KB
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
# time complexity: O(n+q)
# space complexity: O(n)
from typing import List
class Solution:
def sameEndSubstringCount(
self, s: str, queries: List[List[int]]
) -> List[int]:
n = len(s)
charFreqPrefixSum = [[0] * n for _ in range(26)]
for i, char in enumerate(s):
charFreqPrefixSum[ord(char) - ord("a")][i] += 1
for freq in charFreqPrefixSum:
for j in range(1, n):
freq[j] += freq[j - 1]
results = []
for left, right in queries:
countSameEndSubstrings = 0
for freq in charFreqPrefixSum:
leftFreq = 0 if left == 0 else freq[left - 1]
rightFreq = freq[right]
frequencyInRange = rightFreq - leftFreq
countSameEndSubstrings += (
frequencyInRange * (frequencyInRange + 1) // 2
)
results.append(countSameEndSubstrings)
return results
s = "abcaab"
queries = [[0, 0], [1, 4], [2, 5], [0, 5]]
print(Solution().sameEndSubstringCount(s, queries))