From d2f35bd2adc8f97f69a6bd69189ec06f252baada Mon Sep 17 00:00:00 2001 From: Arunanshu Dey Date: Sat, 18 Oct 2025 12:54:27 +0530 Subject: [PATCH 1/5] Add solution for maximizing partitions with bit manipulation Implement a dynamic programming solution to maximize the number of partitions after operations on a string based on bit manipulation. --- ...e_Number_of_Partitions_After_Operations.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py diff --git a/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py b/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py new file mode 100644 index 000000000000..22b9c3ef716c --- /dev/null +++ b/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py @@ -0,0 +1,57 @@ +# Topics: Bit Manuputation, Bit-Mask, DP + + +class Solution: + def __init__(self): + self.dp = {} + + def countSetBits(self, n): + count = 0 + while n > 0: + count += n & 1 + n >>= 1 + return count + + def helper(self, s, k, i, mask, isChange): + n = len(s) + if i == n: + return 1 + + currState = (i << 27) | (mask << 1) | isChange + + if currState in self.dp: + return self.dp[currState] + + result = 0 + val = ord(s[i]) - ord('a') + + count = self.countSetBits(mask | (1 << val)) + temp = 0 + if count > k: + temp = 1 + self.helper(s, k, i + 1, 1 << val, isChange) + else: + temp = self.helper(s, k, i + 1, mask | (1 << val), isChange) + result = max(result, temp) + + if isChange == 0: + for j in range(26): + count = self.countSetBits(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[currState] = result + return result + + def maxPartitionsAfterOperations(self, s, k): + """ + :type s: str + :type k: int + :rtype: int + """ + return self.helper(s, k, 0, 0, 0) + + # Time Complexity: O(n × 2²⁷) + # Auxiliary Space: O(n × 2²⁷) From 59b564b05a7400f4d5e996e6f24fa0a4b0200226 Mon Sep 17 00:00:00 2001 From: Arunanshu Dey Date: Sat, 18 Oct 2025 13:05:42 +0530 Subject: [PATCH 2/5] Refactor method names for consistency and removed whitespaces forrclarity --- ...e_Number_of_Partitions_After_Operations.py | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py b/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py index 22b9c3ef716c..a866d88799e2 100644 --- a/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py +++ b/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py @@ -1,57 +1,46 @@ -# Topics: Bit Manuputation, Bit-Mask, DP - - class Solution: def __init__(self): self.dp = {} - - def countSetBits(self, n): + 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, isChange): + def helper(self, s, k, i, mask, is_change): n = len(s) if i == n: return 1 - - currState = (i << 27) | (mask << 1) | isChange - - if currState in self.dp: - return self.dp[currState] - + 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.countSetBits(mask | (1 << val)) + count = self.count_set_bits(mask | (1 << val)) temp = 0 if count > k: - temp = 1 + self.helper(s, k, i + 1, 1 << val, isChange) + temp = 1 + self.helper(s, k, i + 1, 1 << val, is_change) else: - temp = self.helper(s, k, i + 1, mask | (1 << val), isChange) + temp = self.helper(s, k, i + 1, mask | (1 << val), is_change) result = max(result, temp) - - if isChange == 0: + if is_change == 0: for j in range(26): - count = self.countSetBits(mask | (1 << j)) + 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[currState] = result - return result - - def maxPartitionsAfterOperations(self, s, k): - """ + self.dp[curr_state] = result + return result + def max_partitions_after_operations(self, s, k): + """ :type s: str :type k: int :rtype: int """ return self.helper(s, k, 0, 0, 0) - # Time Complexity: O(n × 2²⁷) - # Auxiliary Space: O(n × 2²⁷) + +# Time Complexity: O(n × 2²⁷) +# Auxiliary Space: O(n × 2²⁷) From 816034f0b11c0c2807e6636d14caacbd03c6de78 Mon Sep 17 00:00:00 2001 From: Arunanshu Dey Date: Sat, 18 Oct 2025 13:12:34 +0530 Subject: [PATCH 3/5] Module name corrected and removed ambiguous character Removed unnecessary comments about time and space complexity. --- ... => Maximize_the_Number_of_Partitions_After_Operations.py} | 4 ---- 1 file changed, 4 deletions(-) rename bit_manipulation/{Maximize _he_Number_of_Partitions_After_Operations.py => Maximize_the_Number_of_Partitions_After_Operations.py} (95%) diff --git a/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py b/bit_manipulation/Maximize_the_Number_of_Partitions_After_Operations.py similarity index 95% rename from bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py rename to bit_manipulation/Maximize_the_Number_of_Partitions_After_Operations.py index a866d88799e2..ded3f0bcc0d1 100644 --- a/bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py +++ b/bit_manipulation/Maximize_the_Number_of_Partitions_After_Operations.py @@ -40,7 +40,3 @@ def max_partitions_after_operations(self, s, k): :rtype: int """ return self.helper(s, k, 0, 0, 0) - - -# Time Complexity: O(n × 2²⁷) -# Auxiliary Space: O(n × 2²⁷) From 8fb952d18879e2cc0757bfbb9d926e81c7374a9a Mon Sep 17 00:00:00 2001 From: Arunanshu Dey Date: Sat, 18 Oct 2025 13:14:11 +0530 Subject: [PATCH 4/5] Rename file to follow lowercase naming convention --- ...s.py => maximize_the_number_of_partitions_after_operations.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bit_manipulation/{Maximize_the_Number_of_Partitions_After_Operations.py => maximize_the_number_of_partitions_after_operations.py} (100%) diff --git a/bit_manipulation/Maximize_the_Number_of_Partitions_After_Operations.py b/bit_manipulation/maximize_the_number_of_partitions_after_operations.py similarity index 100% rename from bit_manipulation/Maximize_the_Number_of_Partitions_After_Operations.py rename to bit_manipulation/maximize_the_number_of_partitions_after_operations.py From 1acb2ffe43b46d05d52e074eb57829a8180cd161 Mon Sep 17 00:00:00 2001 From: Arunanshu Dey Date: Sat, 18 Oct 2025 13:36:36 +0530 Subject: [PATCH 5/5] 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. --- ...mize_the_number_of_partitions_after_operations.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/maximize_the_number_of_partitions_after_operations.py b/bit_manipulation/maximize_the_number_of_partitions_after_operations.py index ded3f0bcc0d1..fc8b0c2df94a 100644 --- a/bit_manipulation/maximize_the_number_of_partitions_after_operations.py +++ b/bit_manipulation/maximize_the_number_of_partitions_after_operations.py @@ -1,12 +1,14 @@ 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: @@ -14,15 +16,17 @@ def helper(self, s, k, i, mask, is_change): 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)) @@ -31,10 +35,12 @@ def helper(self, s, k, i, mask, is_change): else: temp = self.helper(s, k, i + 1, mask | (1 << j), 1) result = max(result, temp) + self.dp[curr_state] = result - return result + return result + def max_partitions_after_operations(self, s, k): - """ + """ :type s: str :type k: int :rtype: int