Skip to content

Commit 59b564b

Browse files
authored
Refactor method names for consistency and removed whitespaces forrclarity
1 parent d2f35bd commit 59b564b

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed
Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,46 @@
1-
# Topics: Bit Manuputation, Bit-Mask, DP
2-
3-
41
class Solution:
52
def __init__(self):
63
self.dp = {}
7-
8-
def countSetBits(self, n):
4+
def count_set_bits(self, n):
95
count = 0
106
while n > 0:
117
count += n & 1
128
n >>= 1
139
return count
14-
15-
def helper(self, s, k, i, mask, isChange):
10+
def helper(self, s, k, i, mask, is_change):
1611
n = len(s)
1712
if i == n:
1813
return 1
19-
20-
currState = (i << 27) | (mask << 1) | isChange
21-
22-
if currState in self.dp:
23-
return self.dp[currState]
24-
14+
curr_state = (i << 27) | (mask << 1) | is_change
15+
if curr_state in self.dp:
16+
return self.dp[curr_state]
2517
result = 0
2618
val = ord(s[i]) - ord('a')
27-
28-
count = self.countSetBits(mask | (1 << val))
19+
count = self.count_set_bits(mask | (1 << val))
2920
temp = 0
3021
if count > k:
31-
temp = 1 + self.helper(s, k, i + 1, 1 << val, isChange)
22+
temp = 1 + self.helper(s, k, i + 1, 1 << val, is_change)
3223
else:
33-
temp = self.helper(s, k, i + 1, mask | (1 << val), isChange)
24+
temp = self.helper(s, k, i + 1, mask | (1 << val), is_change)
3425
result = max(result, temp)
35-
36-
if isChange == 0:
26+
if is_change == 0:
3727
for j in range(26):
38-
count = self.countSetBits(mask | (1 << j))
28+
count = self.count_set_bits(mask | (1 << j))
3929
if count > k:
4030
temp = 1 + self.helper(s, k, i + 1, 1 << j, 1)
4131
else:
4232
temp = self.helper(s, k, i + 1, mask | (1 << j), 1)
4333
result = max(result, temp)
44-
45-
self.dp[currState] = result
46-
return result
47-
48-
def maxPartitionsAfterOperations(self, s, k):
49-
"""
34+
self.dp[curr_state] = result
35+
return result
36+
def max_partitions_after_operations(self, s, k):
37+
"""
5038
:type s: str
5139
:type k: int
5240
:rtype: int
5341
"""
5442
return self.helper(s, k, 0, 0, 0)
5543

56-
# Time Complexity: O(n × 2²⁷)
57-
# Auxiliary Space: O(n × 2²⁷)
44+
45+
# Time Complexity: O(n × 2²⁷)
46+
# Auxiliary Space: O(n × 2²⁷)

0 commit comments

Comments
 (0)