Skip to content

Commit 1acb2ff

Browse files
authored
Removed Trailing whitespace and fix unindent does not match any outer indentation level
Refactor helper function to improve readability and maintainability. Adjusted bit manipulation logic and added comments for clarity.
1 parent 8fb952d commit 1acb2ff

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

bit_manipulation/maximize_the_number_of_partitions_after_operations.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
class Solution:
22
def __init__(self):
33
self.dp = {}
4+
45
def count_set_bits(self, n):
56
count = 0
67
while n > 0:
78
count += n & 1
89
n >>= 1
910
return count
11+
1012
def helper(self, s, k, i, mask, is_change):
1113
n = len(s)
1214
if i == n:
1315
return 1
1416
curr_state = (i << 27) | (mask << 1) | is_change
1517
if curr_state in self.dp:
1618
return self.dp[curr_state]
19+
1720
result = 0
1821
val = ord(s[i]) - ord('a')
1922
count = self.count_set_bits(mask | (1 << val))
20-
temp = 0
23+
2124
if count > k:
2225
temp = 1 + self.helper(s, k, i + 1, 1 << val, is_change)
2326
else:
2427
temp = self.helper(s, k, i + 1, mask | (1 << val), is_change)
2528
result = max(result, temp)
29+
2630
if is_change == 0:
2731
for j in range(26):
2832
count = self.count_set_bits(mask | (1 << j))
@@ -31,10 +35,12 @@ def helper(self, s, k, i, mask, is_change):
3135
else:
3236
temp = self.helper(s, k, i + 1, mask | (1 << j), 1)
3337
result = max(result, temp)
38+
3439
self.dp[curr_state] = result
35-
return result
40+
return result
41+
3642
def max_partitions_after_operations(self, s, k):
37-
"""
43+
"""
3844
:type s: str
3945
:type k: int
4046
:rtype: int

0 commit comments

Comments
 (0)