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
104 changes: 104 additions & 0 deletions ArrayString/array_string_s1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""
Leetcode top interview 150
https://leetcode.com/studyplan/top-interview-150/

Problem links:
1. https://leetcode.com/problems/merge-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150
2. https://leetcode.com/problems/remove-element/?envType=study-plan-v2&envId=top-interview-150
3. https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150
4. https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150
5. https://leetcode.com/problems/majority-element/description/?envType=study-plan-v2&envId=top-interview-150
"""


class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: None Do not return anything, modify nums1 in-place instead.
"""
# TODO: this is not the right solution work on it later
del nums1[-(m - n):]
nums1.extend(nums2)
nums1.sort()
print(nums1)

def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
st = 0
ed = len(nums) - 1
while st <= ed:
if nums[st] == val:
# nums.append(nums.pop(st))
nums.pop(st)
ed -= 1
else:
st += 1
return len(nums)

def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
st = 0
ed = st + 1
while ed < len(nums):
if nums[st] == nums[ed]:
nums.pop(ed)
else:
st = ed
ed += 1
return len(nums)

def removeDuplicates2(self, nums):
"""
:type nums: List[int]
:rtype: int
#TODO complete this solution ans is not correct
"""
st = 0
ed = st + 1
count = 1
while ed < len(nums):
print(count)
if nums[st] != nums[ed]:
count = 1
if nums[st] == nums[ed]:
count += 1
if count > 2:
nums.pop(ed)
ed += 1
else:
st = ed
ed += 1
print(nums)

def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
d = dict()
for num in nums:
if num in d:
d[num] += 1
else:
d[num] = 0

return max(d, key=d.get)


s = Solution()
# s.merge([1, 2, 3, 0, 0, 0], 6, [2, 5, 6], 3)
# s.removeElement([0, 1, 2, 2, 3, 0, 4, 2], 2)
# s.removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4])
# s.removeDuplicates2([0, 0, 1, 1, 1, 1, 2, 3, 3])
s.majorityElement([1, 2, 2, 1, 1, 1, 1, 2, 2])
80 changes: 80 additions & 0 deletions ArrayString/array_string_s2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Leetcode top interview 150
https://leetcode.com/studyplan/top-interview-150/

Problem links:
6. https://leetcode.com/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150
7. https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150
"""


class Solution(object):
def rotate(self, nums, k):
k %= len(nums) # this is to avoid
return nums[-k:] + nums[:-k]

def buy_the_stock(self, bought, bought_day, buy, day):
if buy < bought:
bought = buy
bought_day = day
return bought, bought_day

def sell_the_stock(self, sold, sell):
if sell > sold:
sold = sell
return sold

def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
# TODO: this is not the right solution work on it later
day = 1
sold = prices[-1]
bought = prices[0]
bought_day = 0
while day < len(prices):
bought, bought_day = self.buy_the_stock(bought, bought_day,
prices[day], day)
day += 1
while bought_day < len(prices):
sold = self.sell_the_stock(sold, prices[bought_day])
bought_day += 1
return sold - bought

def jumped_to(self, now_at, nums):
print(f"returning {nums[now_at]}")
return nums[now_at]

def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
# TODO: this is not the right solution work on it later
# i = 0
# if len(nums) == 1 and nums[0] == 0:
# return True
#
# while i <= len(nums):
# if i == nums[-1]:
# return True
# print(i)
# i += self.jumped_to(i, nums)
# else:
# return False

start = 0
end = start + nums[start]

while True:
end = self.jumped_to(start, end)
start = end


s = Solution()
# print(s.rotate([1, 2], 3))
# print(s.maxProfit([2, 4, 1]))
print(s.canJump([3, 2, 1, 0, 4]))
# [2,3,1,1,4]
135 changes: 135 additions & 0 deletions HashMap/hashmap_s1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"""
Leetcode top interview 150
https://leetcode.com/studyplan/top-interview-150/

Problem links:
1. https://leetcode.com/problems/ransom-note/description/?envType=study-plan-v2&envId=top-interview-150
2. https://leetcode.com/problems/isomorphic-strings/description/?envType=study-plan-v2&envId=top-interview-150
3. https://leetcode.com/problems/word-pattern/description/?envType=study-plan-v2&envId=top-interview-150
4. https://leetcode.com/problems/valid-anagram/description/?envType=study
-plan-v2&envId=top-interview-150

"""


class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool

this solution on leetcode took:
14 ms Beats 99.75% of users with Python
"""
ran_lett = set(ransomNote)
for letter in ran_lett:
if ransomNote.count(letter) > magazine.count(letter):
return False
return True

def get_count_and_all_index(self, string, ch):
str_count = string.count(ch)
indx = [idx for idx, ltr in enumerate(string) if ltr == ch]
return str_count, indx

def isIsomorphic(self, r, t):
"""
:type s: str
:type t: str
:rtype: bool
# TODO: Solution is having worst time complexity
"""
str_len = len(t)
while str_len > 0:
# for a, b in zip(r, t):
# r_count_indx = s.get_count_and_all_index(r, a)
# t_count_indx = s.get_count_and_all_index(t, b)
# if r_count_indx == t_count_indx:
# r = r.replace(a, "")
# t = t.replace(b, "")
# str_len -= r_count_indx[0]
# else:
# return False
for a, b in zip(r, t):
r_idx = [idx for idx, ltr in enumerate(r) if ltr == a]
t_idx = [idx for idx, ltr in enumerate(t) if ltr == b]
if r_idx == t_idx:
r = r.replace(a, "")
t = t.replace(b, "")
str_len -= len(r_idx)
else:
return False
return True

def wordPattern(self, pattern, s):
"""
:type pattern: str
:type s: str
:rtype: bool
TODO: Sol failing for test case discuss with Abhi
test case: "aaa", "aa aa aa aa"
"""
dt = {}
for pt, ch in zip(pattern, s.split(" ")):
if pt not in dt and ch not in dt.values():
dt[pt] = ch
elif (pt in dt and dt[pt] != ch) or (
ch in dt.values() and pt not in dt):
return False
return True

def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t):
return False

# s = [*s]
# t = [*t]
# s.sort()
# t.sort()
# if s == t:
# return True
# else:
# return False
sd = {}
td = {}
for l, c in zip(s, t):
if l not in sd:
sd[l] = s.count(l)
if c not in td:
td[c] = t.count(c)
if sd == td:
return True
else:
return False

def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
groups = {}
if len(strs) == 1 or len(set(strs)) == 1:
return [strs]

for ch in strs:
sw = ''.join(sorted(ch))
if sw in groups:
groups[sw].append(ch)
else:
groups[sw] = ([ch])
return groups.values()


so = Solution()
# print(so.canConstruct('aa',
# 'aab'))
# print(so.isIsomorphic("foo", "bar"))
# print(so.wordPattern("aaaa", "aa aa aa aa"))
# print(so.isAnagram("a", "ab"))
print(so.groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]))
Loading