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
41 changes: 41 additions & 0 deletions GroupAnagrams.py
Original file line number Diff line number Diff line change
@@ -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())


25 changes: 25 additions & 0 deletions IsomorphicStrings.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions WordPattern.py
Original file line number Diff line number Diff line change
@@ -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