diff options
Diffstat (limited to '1_array_hashing/valid_anagram.py')
| -rw-r--r-- | 1_array_hashing/valid_anagram.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/1_array_hashing/valid_anagram.py b/1_array_hashing/valid_anagram.py new file mode 100644 index 0000000..1c8a1d8 --- /dev/null +++ b/1_array_hashing/valid_anagram.py @@ -0,0 +1,79 @@ +""" +Question + +Is Anagram + +Easy + +Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false. + +An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different. + +Example 1: + +Input: s = "racecar", t = "carrace" + +Output: true + +Example 2: + +Input: s = "jar", t = "jam" + +Output: false + +Constraints: + - s and t consist of lowercase English letters. +""" + +class Solution: + def hashmap(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + countS, countT = {}, {} + for i in range(len(s)): + countS[s[i]] = 1 + countS.get(s[i], 0) + countT[t[i]] = 1 + countT.get(t[i], 0) + for c in countS: + if countS[c] != countT.get(c, 0): + return False + return True + + +case1S = "apple" +case1T = "plpea" +case2S = "asneaxl" +case2T = "yejindp" +case3S = "iloveyou" +case3T = "youlovei" + +solution = Solution() + +print(f"hashmap case1: {solution.hashmap(case1S, case1T)}") +print(f"hashmap case2: {solution.hashmap(case2S, case2T)}") +print(f"hashmap case3: {solution.hashmap(case3S, case3T)}") + +""" +Solution +url: https://neetcode.io/problems/is-anagram +video: https://youtu.be/9UtInBqnCgA + +1. brute force +time: O(n) +space: O(1) + +2. hashmap +time: O(s+t) +space: O(s+t) +code: +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + countS, countT = {}, {} + + for i in range(len(s)): + countS[s[i]] = 1 + countS.get(s[i], 0) + countT[t[i]] = 1 + countT.get(t[i], 0) + return countS == countT +""" |
