From 257065aca121368210a6923eb53d899102cf5d0a Mon Sep 17 00:00:00 2001 From: amitmittal117 Date: Mon, 2 Feb 2026 17:11:19 -0800 Subject: [PATCH] [IMP] Hashing-1 --- anagrams.py | 16 ++++++++++++++++ isomorphic-strings.py | 21 +++++++++++++++++++++ word-pattern.py | 21 +++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 anagrams.py create mode 100644 isomorphic-strings.py create mode 100644 word-pattern.py 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