diff --git a/anagrams.py b/anagrams.py new file mode 100644 index 00000000..690933b2 --- /dev/null +++ b/anagrams.py @@ -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()) diff --git a/isomorphic-strings.py b/isomorphic-strings.py new file mode 100644 index 00000000..3e85b733 --- /dev/null +++ b/isomorphic-strings.py @@ -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 \ No newline at end of file diff --git a/word-pattern.py b/word-pattern.py new file mode 100644 index 00000000..81214642 --- /dev/null +++ b/word-pattern.py @@ -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