From 4ae9fe55495940db1c557e4491e66f64988ad624 Mon Sep 17 00:00:00 2001 From: Neeraj Date: Mon, 12 Jan 2026 18:53:31 -0800 Subject: [PATCH] Add Python solutions for all three hashing-2 problems --- Problem1_SubarraySumEqualsK.py | 27 ++++++++++++++++++++++++++ Problem2_ContiguousArray.py | 28 +++++++++++++++++++++++++++ Problem3_LongestPalindrome.py | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 Problem1_SubarraySumEqualsK.py create mode 100644 Problem2_ContiguousArray.py create mode 100644 Problem3_LongestPalindrome.py diff --git a/Problem1_SubarraySumEqualsK.py b/Problem1_SubarraySumEqualsK.py new file mode 100644 index 00000000..1e26e987 --- /dev/null +++ b/Problem1_SubarraySumEqualsK.py @@ -0,0 +1,27 @@ +# Time Complexity : O(n) where n is the length of the array +# Space Complexity : O(n) for storing cumulative sums in hashmap +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Use a hashmap to store cumulative sums and their frequencies as we iterate through the array. +# For each element, calculate the cumulative sum and check if (cumulative_sum - k) exists in the hashmap. +# If it exists, it means there are subarrays ending at current index with sum k, so add their count to result. + +class Solution: + def subarraySum(self, nums: List[int], k: int) -> int: + count = 0 + cumulative_sum = 0 + sum_map = {0: 1} # Initialize with sum 0 having count 1 + + for num in nums: + cumulative_sum += num + + # Check if (cumulative_sum - k) exists in map + if (cumulative_sum - k) in sum_map: + count += sum_map[cumulative_sum - k] + + # Update the frequency of current cumulative sum + sum_map[cumulative_sum] = sum_map.get(cumulative_sum, 0) + 1 + + return count + diff --git a/Problem2_ContiguousArray.py b/Problem2_ContiguousArray.py new file mode 100644 index 00000000..582befe2 --- /dev/null +++ b/Problem2_ContiguousArray.py @@ -0,0 +1,28 @@ +# Time Complexity : O(n) where n is the length of the array +# Space Complexity : O(n) for storing cumulative sum indices in hashmap +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Transform the problem by treating 0s as -1 and 1s as +1, making it equivalent to finding longest subarray with sum 0. +# Use a hashmap to store the first occurrence index of each cumulative sum encountered. +# When the same cumulative sum appears again, calculate the length between current and stored index to find equal 0s and 1s. + +class Solution: + def findMaxLength(self, nums: List[int]) -> int: + sum_map = {0: -1} # Initialize with sum 0 at index -1 + max_length = 0 + cumulative_sum = 0 + + for i, num in enumerate(nums): + # Treat 0 as -1 and 1 as +1 + cumulative_sum += 1 if num == 1 else -1 + + # If this cumulative sum was seen before, calculate length + if cumulative_sum in sum_map: + max_length = max(max_length, i - sum_map[cumulative_sum]) + else: + # Store first occurrence of this cumulative sum + sum_map[cumulative_sum] = i + + return max_length + diff --git a/Problem3_LongestPalindrome.py b/Problem3_LongestPalindrome.py new file mode 100644 index 00000000..c6566dbb --- /dev/null +++ b/Problem3_LongestPalindrome.py @@ -0,0 +1,35 @@ +# Time Complexity : O(n) where n is the length of the string +# Space Complexity : O(1) since hashmap has at most 52 entries (uppercase and lowercase letters) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Count frequency of each character using a hashmap to track occurrences. +# For each character, add the largest even number of occurrences (count // 2 * 2) to the palindrome length. +# If any character has an odd count, add one more character to the center of the palindrome. + +class Solution: + def longestPalindrome(self, s: str) -> int: + char_count = {} + + # Count frequency of each character + for char in s: + char_count[char] = char_count.get(char, 0) + 1 + + length = 0 + odd_found = False + + # Calculate palindrome length + for count in char_count.values(): + # Add largest even number of characters + length += count // 2 * 2 + + # Check if there's an odd count character for center + if count % 2 == 1: + odd_found = True + + # Add one center character if any odd count exists + if odd_found: + length += 1 + + return length +