Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions Problem1_SubarraySumEqualsK.py
Original file line number Diff line number Diff line change
@@ -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

28 changes: 28 additions & 0 deletions Problem2_ContiguousArray.py
Original file line number Diff line number Diff line change
@@ -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

35 changes: 35 additions & 0 deletions Problem3_LongestPalindrome.py
Original file line number Diff line number Diff line change
@@ -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