diff --git a/Problem1.cpp b/Problem1.cpp deleted file mode 100644 index 8b137891..00000000 --- a/Problem1.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem1.java b/Problem1.java deleted file mode 100644 index 8b137891..00000000 --- a/Problem1.java +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem2.cpp b/Problem2.cpp deleted file mode 100644 index 8b137891..00000000 --- a/Problem2.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem2.java b/Problem2.java deleted file mode 100644 index 8b137891..00000000 --- a/Problem2.java +++ /dev/null @@ -1 +0,0 @@ - diff --git a/problem11.py b/problem11.py new file mode 100644 index 00000000..b714a6fd --- /dev/null +++ b/problem11.py @@ -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) \ No newline at end of file diff --git a/problem14.py b/problem14.py new file mode 100644 index 00000000..85bcebf9 --- /dev/null +++ b/problem14.py @@ -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 \ No newline at end of file diff --git a/problem15.py b/problem15.py new file mode 100644 index 00000000..d824a64c --- /dev/null +++ b/problem15.py @@ -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. \ No newline at end of file diff --git a/problem16.py b/problem16.py new file mode 100644 index 00000000..b5c53fe7 --- /dev/null +++ b/problem16.py @@ -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) \ No newline at end of file