diff --git a/leetcode409.py b/leetcode409.py new file mode 100644 index 00000000..aa7e5171 --- /dev/null +++ b/leetcode409.py @@ -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) \ No newline at end of file diff --git a/leetcode525.py b/leetcode525.py new file mode 100644 index 00000000..1362dc08 --- /dev/null +++ b/leetcode525.py @@ -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 \ No newline at end of file diff --git a/leetcode560.py b/leetcode560.py new file mode 100644 index 00000000..f92b42a6 --- /dev/null +++ b/leetcode560.py @@ -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 \ No newline at end of file