diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..a5fa0e47 --- /dev/null +++ b/problem1.py @@ -0,0 +1,19 @@ +## Problem1 (https://leetcode.com/problems/subarray-sum-equals-k/) + +class Solution: + def subarraySum(self, nums: List[int], k: int) -> int: + r_sum = 0 + hm = { + 0:1 + } + count = 0 + for num in nums: + r_sum += num + comp = r_sum - k + if comp in hm: + count+=hm[comp] + if r_sum in hm: + hm[r_sum]+=1 + else: + hm[r_sum]=1 + return count \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..b5c8e981 --- /dev/null +++ b/problem2.py @@ -0,0 +1,20 @@ +## Problem2 (https://leetcode.com/problems/contiguous-array/) + +class Solution: + def findMaxLength(self, nums: List[int]) -> int: + r_sum = 0 + max_length = 0 + my_map = { + 0:-1 + } + for i in range(len(nums)): + if nums[i] == 0: + r_sum-=1 + else: + r_sum+=1 + + if r_sum in my_map: + max_length = max(max_length,i-my_map[r_sum]) + else: + my_map[r_sum] = i + return max_length diff --git a/problem3.py b/problem3.py new file mode 100644 index 00000000..122c92a0 --- /dev/null +++ b/problem3.py @@ -0,0 +1,15 @@ +## Problem3 (https://leetcode.com/problems/longest-palindrome) + +class Solution: + def longestPalindrome(self, s: str) -> int: + my_set = set() + count = 0 + for i in s: + if i in my_set: + count+=2 + my_set.remove(i) + else: + my_set.add(i) + if len(my_set) > 0: + return count+1 + return count \ No newline at end of file