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
16 changes: 16 additions & 0 deletions anagrams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'''
Time Complexity : O(nklogk)
Space Complexity : O(nk)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''
class Solution:
def groupAnagrams(self, strs):
ans = {}

for s in strs:
key = "".join(sorted(s))
ans[key] = ans.get(key, [])
ans[key].append(s)

return list(ans.values())
21 changes: 21 additions & 0 deletions isomorphic-strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Time Complexity : O(n)
Space Complexity : O(1)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''

class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
hashmap = {}
taken = []
for i in range(len(s)):
if s[i] in hashmap:
if hashmap[s[i]] != t[i]:
return False
else:
if t[i] in taken:
return False
taken.append(t[i])
hashmap[s[i]] = t[i]
return True
21 changes: 21 additions & 0 deletions word-pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Time Complexity : O(n)
Space Complexity : O(1)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
hashmap = {}
s = s.split(" ")
if len(pattern) != len(s):
return False
for i in range(len(pattern)):
if pattern[i] in hashmap:
if s[i] != hashmap[pattern[i]]:
return False
elif s[i] in hashmap.values():
return False
else:
hashmap[pattern[i]] = s[i]
return True