diff --git a/GroupAnagrams.py b/GroupAnagrams.py new file mode 100644 index 00000000..cb1b946c --- /dev/null +++ b/GroupAnagrams.py @@ -0,0 +1,41 @@ +# Time Complexity : O(n * k) ignoring method to get prime numbers as it is a helper function with TC : sqrt(n) +# Space Complexity : O(n * k) where n is number of strings and k is max length of string +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : Hash each string as a product of prime numbers corresponding to each character. + +class Solution: + def first_26_primes(self): + primes = [] + num = 2 + while len(primes) < 26: + is_prime = True + for i in range(2, int(num ** 0.5) + 1): + if num % i == 0: + is_prime = False + break + if is_prime: + primes.append(num) + num += 1 + return primes + + def getHash(self, key: str) -> float: + primes = self.first_26_primes() + + hash = 1.0 + + for ch in key: + hash *= primes[ord(ch) - ord('a')] + + return hash + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + groupedStrs = defaultdict(list) + + for s in strs: + key = self.getHash(s) + groupedStrs[key].append(s) + + return list(groupedStrs.values()) + + \ No newline at end of file diff --git a/IsomorphicStrings.py b/IsomorphicStrings.py new file mode 100644 index 00000000..f4e6b9a1 --- /dev/null +++ b/IsomorphicStrings.py @@ -0,0 +1,25 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : Store mappings of s to t in an array and track the mapped values. + +class Solution: + def isIsomorphic(self, s: str, t: str) -> bool: + sMap = ['\0'] * 100 + sMappedValues = [False] * 100 + + for i in range(len(s)): + sChar = s[i] + tChar = t[i] + + if sMap[ord(sChar) - ord(' ')] != '\0': + if sMap[ord(sChar) - ord(' ')] != tChar: + return False + else: + if sMappedValues[ord(tChar) - ord(' ')]: + return False + sMap[ord(sChar) - ord(' ')] = tChar + sMappedValues[ord(tChar) - ord(' ')] = True + + return True \ No newline at end of file diff --git a/WordPattern.py b/WordPattern.py new file mode 100644 index 00000000..68c253ee --- /dev/null +++ b/WordPattern.py @@ -0,0 +1,32 @@ +# Time Complexity : O(n + m) where n is length of s and m is length of pattern +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : Map each character and word to its index. If it already exists then there will be index mismatch. + +class Solution: + def wordPattern(self, pattern: str, s: str) -> bool: + map_index = {} + words = s.split() + + if len(pattern) != len(words): + return False + + for i in range(len(words)): + ch = pattern[i] + word = words[i] + + char_key = 'char_{}'.format(ch) + char_word = 'word_{}'.format(word) + + if char_key not in map_index: + map_index[char_key] = i + + if char_word not in map_index: + map_index[char_word] = i + + if map_index[char_key] != map_index[char_word]: + return False + + return True + \ No newline at end of file