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,46 @@
class Solution:

Check failure on line 1 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:1:1: N999 Invalid module name: 'Maximize _he_Number_of_Partitions_After_Operations'
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 _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/Maximize _he_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 _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

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


# Time Complexity: O(n × 2²⁷)

Check failure on line 45 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF003)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:45:24: RUF003 Comment contains ambiguous `×` (MULTIPLICATION SIGN). Did you mean `x` (LATIN SMALL LETTER X)?
# Auxiliary Space: O(n × 2²⁷)

Check failure on line 46 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF003)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:46:24: RUF003 Comment contains ambiguous `×` (MULTIPLICATION SIGN). Did you mean `x` (LATIN SMALL LETTER X)?
Loading