algos_and_structures/neetcode/is_anagram/main.py
2024-11-02 14:03:30 +03:00

41 lines
1.3 KiB
Python

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s_chars = dict()
t_chars = dict()
for s_char in s:
new_char_check = s_chars.get(s_char)
if not new_char_check:
s_chars[s_char] = 1
else:
s_chars[s_char] += 1
for t_char in t:
new_char_check = t_chars.get(t_char)
if not new_char_check:
t_chars[t_char] = 1
else:
t_chars[t_char] += 1
return True if s_chars == t_chars else False
class NeetCodeSolution:
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
entry_1 = {"s": "racecar", "t": "carrace", "expected": True}
entry_2 = {"s": "jar", "t": "jam", "expected": False}
# entry_3 = {"s": "racecar", "t": "carrace", "expected": True}
# entry_4 = {"s": "racecar", "t": "carrace", "expected": True}
# entry_5 = {"s": "racecar", "t": "carrace", "expected": True}
s = Solution()
print(s.isAnagram(entry_1["s"], entry_1["t"]))
print(s.isAnagram(entry_2["s"], entry_2["t"]))