-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2168-unique-substrings-with-equal-digit-frequency.py
More file actions
38 lines (32 loc) · 1.26 KB
/
2168-unique-substrings-with-equal-digit-frequency.py
File metadata and controls
38 lines (32 loc) · 1.26 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
35
36
37
38
# time complexity: O(n^2)
# space complexity: O(n^2)
class Solution:
class Trie:
def __init__(self):
self.children = [None] * 10
self.isVisited = False
def equalDigitFrequency(self, s: str) -> int:
root = self.Trie()
totalValidSubstrings = 0
for left in range(len(s)):
currentNode = root
digitFreq = [0] * 10
uniDigitCount = 0
maxDigitFreq = 0
for right in range(left, len(s)):
currentDigit = int(s[right])
if digitFreq[currentDigit] == 0:
uniDigitCount += 1
digitFreq[currentDigit] += 1
maxDigitFreq = max(maxDigitFreq, digitFreq[currentDigit])
if not currentNode.children[currentDigit]:
currentNode.children[currentDigit] = self.Trie()
currentNode = currentNode.children[currentDigit]
if uniDigitCount * maxDigitFreq == right - left + 1 and not currentNode.isVisited:
totalValidSubstrings += 1
currentNode.isVisited = True
return totalValidSubstrings
s = "1212"
print(Solution().equalDigitFrequency(s))
s = "12321"
print(Solution().equalDigitFrequency(s))