Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution:
def __init__(self):
self.dp = {}
def count_set_bits(self, n):
count = 0
while n > 0:
count += n & 1
n >>= 1
return count
def helper(self, s, k, i, mask, is_change):
n = len(s)
if i == n:
return 1
curr_state = (i << 27) | (mask << 1) | is_change
if curr_state in self.dp:
return self.dp[curr_state]
result = 0
val = ord(s[i]) - ord('a')
count = self.count_set_bits(mask | (1 << val))
temp = 0
if count > k:
temp = 1 + self.helper(s, k, i + 1, 1 << val, is_change)
else:
temp = self.helper(s, k, i + 1, mask | (1 << val), is_change)
result = max(result, temp)
if is_change == 0:
for j in range(26):
count = self.count_set_bits(mask | (1 << j))
if count > k:
temp = 1 + self.helper(s, k, i + 1, 1 << j, 1)
else:
temp = self.helper(s, k, i + 1, mask | (1 << j), 1)
result = max(result, temp)
self.dp[curr_state] = result
return result

Check failure on line 35 in bit_manipulation/maximize_the_number_of_partitions_after_operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/maximize_the_number_of_partitions_after_operations.py:35:22: W291 Trailing whitespace
def max_partitions_after_operations(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
return self.helper(s, k, 0, 0, 0)

Check failure on line 42 in bit_manipulation/maximize_the_number_of_partitions_after_operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

bit_manipulation/maximize_the_number_of_partitions_after_operations.py:42:1: invalid-syntax: unindent does not match any outer indentation level
Loading