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
23 changes: 23 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# LEETCODE PROBLEM 560 SUBARRAY SUM EQUALS K
# TIME COMPLEXITY:O(N) where N is the number of elements
# SPACE COMPLEXITY: O(N) where N is the space required to make a hashmap of N elements
# Any problem you faced while coding this: None

class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
map={0:1}
runningSum=0
count=0
for i in range(len(nums)):
runningSum+=nums[i]
diff=runningSum-k
if diff in map:
count+=map[diff]
map[runningSum]=map.get(runningSum,0)+1
return count

24 changes: 24 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# LEETCODE PROBLEM 525 CONTIGUOUS ARRAY
# TIME COMPLEXITY: O(N) where N is the number of elements
# SPACE COMPLEXITY: O(N) given that we need to initiate a hashmap for N elements
# Any problem you faced while coding this: None

class Solution(object):
def findMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
runningSum=0
map={0:-1}
result=0
for i in range(len(nums)):
if nums[i]==0:
runningSum-=1
else:
runningSum+=1
if runningSum in map:
result=max(result,i-map[runningSum])
else:
map[runningSum]=i
return result
24 changes: 24 additions & 0 deletions problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# LEETCODE PROBLEM 409 LONGEST PALINDROME
# TIME COMPLEXITY: O(N) where N is the number of elements in a given array
# SPACE COMPLEXITY: O(1)
# Any problem you faced while coding this: None

class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
set_char=set()
count=0

for i in s:
if i in set_char:
count+=2
set_char.remove(i)
else:
set_char.add(i)
if len(set_char)>0:
return count+1
return count