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
1 change: 0 additions & 1 deletion Problem1.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Problem1.java

This file was deleted.

1 change: 0 additions & 1 deletion Problem2.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Problem2.java

This file was deleted.

46 changes: 46 additions & 0 deletions problem11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#Here assuming one element is missing for sure.

#version 1
def missingNumber(arr):
low = 0
high = len(arr)-1

while low <= high:
mid = low + (high-low)//2

if arr[mid] == mid+1:
low = mid+1
else:
high = mid-1
if low == len(arr):#if nothing is missing
return -1
return low+1 #can be mid+1 as well

#how to write the code if all elements are present and nothing is missing?

if __name__ == "__main__":
arr = [1, 2, 3, 4, 5, 6, 8]
print(missingNumber(arr))

#version 2
def missingNumber(arr):
low = 0
high = len(arr)-1

while low < high:
mid = low + (high-low)//2

if arr[mid] == mid+1:
low = mid+1
else:
high = mid
if arr[low] == low + 1:
return -1 #nothing is missing, low won't reach end as it exists before low == high
return low+1

if __name__ == "__main__":
arr = [1, 2, 3, 4, 5, 6, 8]
print(missingNumber(arr))

#Time Complexity O(logn)
# Space Complexity: O(1)
31 changes: 31 additions & 0 deletions problem14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from collections import defaultdict
from typing import List

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
if not strs:
return None

hashMap = defaultdict(list)
res = []

for str in strs:
#26 length array initialize
key = [0]*26

for char in str:
key[ord(char)-ord('a')] += 1

hashMap[tuple(key)].append(str)

for key,values in hashMap.items():
res.append(values)

return res

# Time Complexity: O(n*k + n) = O(N*k)
# Space Complexity: O(nK + n + nk) = O(n*k) worse case: hashMap has n keys of O(1) and values it stores all
# the strings (nk)

# n = number of strings
# k = average length of each string
32 changes: 32 additions & 0 deletions problem15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#can maintain mapping in LL and arry list also but for
#O(1) search complexity, we need hashMap
#we need s to t and t to s mapping to avoid breach of "No two characters may map to the same character". eg: egl and add , here g and l can map to d. To avoid this we need 2 hashmaps
#we can't have a single map for s->t and t->s mapping because we won't know from which string the chars are coming from. Else we have to maintain a flag (in a tuple with key) with keys to show distinction but its as good as having two hashmaps. space complexity will be 2 * no. of keys, if we use 2 hashmaps or two keys within a single hashmap.

from collections import defaultdict

class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False

hashMap_s = defaultdict(str)
hashMap_t = defaultdict(str)

for i in range(len(s)):
if hashMap_s[s[i]]:
if hashMap_s[s[i]] != t[i]:
return False #breach
else:
hashMap_s[s[i]] = t[i]

if hashMap_t[t[i]]:
if hashMap_t[t[i]] != s[i]:
return False #breach
else:
hashMap_t[t[i]] = s[i]

return True

# Time Complexity : O(N) length of string
# Space Complexity : O(1) as worse case it will store only 26 characters O(26) is 1.
26 changes: 26 additions & 0 deletions problem16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
words = s.split() #O(N) N is the length of string

#M is the len of words or length of pattern

if len(pattern) != len(words):
return False

char_to_word = {} #O(M)
word_to_char = {} #O(M)

for ch, word in zip(pattern, words): #O(M)
if ch in char_to_word and char_to_word[ch] != word:
return False

if word in word_to_char and word_to_char[word] != ch:
return False

char_to_word[ch] = word
word_to_char[word] = ch

return True

# Time Complexity: O(N) + O(M) which is O(N)
# Space Complexity: O(M) (list words) + O(M) + O(M)