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
39 changes: 39 additions & 0 deletions leetcode409.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 10 21:21:02 2026

@author: rishigoswamy


Problem: Longest Palindrome
Link: https://leetcode.com/problems/longest-palindrome/

Description:
Given a string s consisting of uppercase and lowercase letters,
return the length of the longest palindrome that can be built
with those letters.

Approach:
Use a HashSet to track characters with odd frequency.

- If a character appears twice, it contributes +2 to length.
- Characters left in the set at the end have odd frequency.
- We can place exactly one odd-frequency character in the center.

Time Complexity: O(n)
Space Complexity: O(1) # At most 52 characters (A-Z, a-z)
"""

class Solution:
def longestPalindrome(self, s: str) -> int:
hSet = set()
length = 0
for char in s:
if char in hSet:
length += 2
hSet.remove(char)
else:
hSet.add(char)

return length + (1 if len(hSet) > 0 else 0)
44 changes: 44 additions & 0 deletions leetcode525.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 10 21:19:45 2026

@author: rishigoswamy

Problem: Contiguous Array
Link: https://leetcode.com/problems/contiguous-array/

Description:
Given a binary array nums, return the maximum length
of a contiguous subarray with an equal number of 0 and 1.

Approach:
Convert 0 → -1.
Then the problem becomes:
Find the longest subarray with sum = 0.

Use Prefix Sum + HashMap to store the first occurrence
of each prefix sum.

Time Complexity: O(n)
Space Complexity: O(n)


"""

from typing import List

class Solution:
def findMaxLength(self, nums: List[int]) -> int:
prefixSum = 0
hMap = {}
hMap[0] = -1
res = 0
for i in range(len(nums)):
prefixSum += 1 if nums[i] == 1 else -1

if prefixSum in hMap:
res = max(res , i - hMap[prefixSum])
else:
hMap[prefixSum] = i
return res
38 changes: 38 additions & 0 deletions leetcode560.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 10 21:17:29 2026

@author: rishigoswamy

Problem: Subarray Sum Equals K
Link: https://leetcode.com/problems/subarray-sum-equals-k/

Approach:
Use Prefix Sum + HashMap to store frequency of prefix sums.

Time Complexity: O(n)
Space Complexity: O(n)

"""

from typing import List

class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
count = 0
hMap = {}
prefixSum = 0

for item in nums:
prefixSum += item
if prefixSum == k:
count+=1
if prefixSum - k in hMap:
count+= hMap[prefixSum - k]
if prefixSum not in hMap:
hMap[prefixSum] = 1
else:
hMap[prefixSum]+= 1

return count