diff --git a/leetcode-409.py b/leetcode-409.py new file mode 100644 index 00000000..f1fbe817 --- /dev/null +++ b/leetcode-409.py @@ -0,0 +1,23 @@ +''' +https://leetcode.com/problems/longest-palindrome/ + +Time Complexity: O(n) +Space comlexity: O(n) +TIP: Count the total number of even occurences and add it to the totalSum. For odd occurences add `occurence - 1`, and set the oddPresent to true - in the end add 1 to res if oddPresent == true +Refer to optimized way in the video solution s30 to use hashset for adding count +''' +from collections import Counter +class Solution: + def longestPalindrome(self, s: str) -> int: + count = Counter(s) + res = 0 + oddPresent = False + for el in count.values(): + if el%2 == 0: + res += el + else: + res += (el - 1) + oddPresent = True + if oddPresent: + res += 1 + return res \ No newline at end of file diff --git a/leetcode-525.py b/leetcode-525.py new file mode 100644 index 00000000..375e0e86 --- /dev/null +++ b/leetcode-525.py @@ -0,0 +1,31 @@ +''' +https://leetcode.com/problems/contiguous-array/description/ + +Time Complexity: O(n) +Space complexity: O(n) + +TIP: brute force: n*n loop to generate subarrays -> and finding the longest canditate. starting from each index in the array. +How do you eliminate the n*n loop? use running sum to remove the iteration.. +since its 0-indexed - you dont need to add an extra 1 to find the diff in the length. this is wrong - i - minIndex[currSum] + 1 +''' +class Solution: + def findMaxLength(self, nums: List[int]) -> int: + #when you encounter 0 -> add -1 to running sum - handles edge case, which says when we start the iteration, sum is initially 0 at index -1 + runningSum = [] + minIndex = {} + minIndex[0] = -1 #handle edge case for first occurence of 0 sum. + currSum = 0 + longestLength = 0 + for i, el in enumerate(nums): + if el == 0: + currSum -= 1 + else: + currSum += 1 + + # runningSum.append(currSum) + if currSum not in minIndex: + minIndex[currSum] = i + else: + longestLength = max(longestLength, i - minIndex[currSum] ) + + return longestLength diff --git a/leetcode-560.py b/leetcode-560.py new file mode 100644 index 00000000..b70ea459 --- /dev/null +++ b/leetcode-560.py @@ -0,0 +1,31 @@ +''' +https://leetcode.com/problems/subarray-sum-equals-k/ + + +Time Complexity: O(n) +Space Complexity: O(n) +TIP: +You need to store dictionary as {runningSum: count} cuz you are tyring to count the occurence of the sum. which will give the total count +''' +class Solution: + def subarraySum(self, nums: List[int], target: int) -> int: + runningSumDict = {} + runningSumDict[0] = 1 #edge case - refer to the video to understand the edge case + runningSum = 0 + totalCount = 0 + + for el in nums: + runningSum += el + complement = runningSum - target + + if complement in runningSumDict: + totalCount += runningSumDict[complement] + + if runningSum not in runningSumDict: + runningSumDict[runningSum] = 1 + else: + runningSumDict[runningSum] += 1 + + return totalCount + + \ No newline at end of file