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
54 changes: 54 additions & 0 deletions groupAnagrams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -------- Solution 1 : By sorting the strings------
''' Time Complexity : O(nklogk) : where n = no of strings in the input
k = maximum/average length of the string
O(n) = to process all strings in input, O(klogk) = to sort individual string
Space Complexity : O(nk) :
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach

Approach : 1. Sort each string in the input
2. create the index of sorted string and Check if it is already mapped in hashmap,
3. append the input string to sorted string index
'''

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
hashmap=defaultdict(list)
for s in strs:
ss = "".join(sorted(s))
hashmap[ss].append(s)
result=[]
for v in hashmap.values():
result.append(v)
return result


# -------- Solution 2 : By hashing using Prime numbers------
''' Time Complexity : O(nk) : where n = no of strings in the input
k = maximum/average length of the string
Space Complexity : O(nk) :
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach

Approach : 1.Create a unique key by multiplying prime nos assigned to each char
2. Append the s which has unique keys
'''

from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
hashmap=defaultdict(list)
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101 ]

for s in strs:
key = 1
for c in s:
key *= primes[ord(c) - ord('a')]
hashmap[key].append(s)
return list(hashmap.values())
32 changes: 32 additions & 0 deletions isIsomorphic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
''' Time Complexity : O(n) , where n = length of the string s or t
Space Complexity : O(1) , hashmap will save mapping of max 26 char irrespective of string length, hence constant time.
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach

Approach : Using two hashmap, one for mapping s to t, and second for mapping t to s
Iterating the strings, and checking if the char is previously mapped to some else char if yes, return False

'''
Class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
shash, thash = {}, {}
for i in range(len(s)):
schar, tchar = s[i],t[i]
# s to t mapping
if schar in shash:
if shash[schar] != tchar:
return False
else:
shash[schar] = tchar

# t to s mapping
if tchar in thash:
if thash[tchar] != schar:
return False
else:
thash[tchar] = schar
return True
40 changes: 40 additions & 0 deletions word_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
''' Time Complexity : O(n) , where n = length of the string s or pattern
Space Complexity : O(1) , hashmap will save mapping of max 26 char irrespective of string length, hence constant time.
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach

Approach : Using two hashmap, one for mapping pattern to s, and second for vice verca
Iterating the strings, and checking if the char is previously mapped to some else word if yes, return False

'''
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
s = s.split()
if len(pattern) != len(s):
return False
# for pattern to s mapping

phashmap = {}
for i in range(len(pattern)):
c = pattern[i]
if c in phashmap:
if phashmap[c] != s[i]:
return False
else:
phashmap[c] = s[i]

# for s to pattern mapping
shashmap = {}
for i in range(len(s)):
word = s[i]
if word in shashmap:
if shashmap[word] != pattern[i]:
return False
else:
shashmap[word] = pattern[i]
return True