68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
"""
|
|
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
|
|
|
|
Example 1:
|
|
Input: s = "anagram", t = "nagaram"
|
|
Output: true
|
|
|
|
Example 2:
|
|
Input: s = "rat", t = "car"
|
|
Output: false
|
|
|
|
Constraints:
|
|
1 <= s.length, t.length <= 5 * 104
|
|
s and t consist of lowercase English letters.
|
|
|
|
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
|
|
"""
|
|
|
|
"""
|
|
Анаграмма - слово составленное из одинакового кол-ва букв
|
|
Поулчается что по сути - тут надо делать счетчик букв в каждом слове через хэшмапу
|
|
"""
|
|
from collections import defaultdict
|
|
|
|
class Solution:
|
|
def isAnagram(self, s: str, t: str) -> bool:
|
|
if len(s) != len(t):
|
|
return False
|
|
word_1 = defaultdict(int)
|
|
word_2 = defaultdict(int)
|
|
for char in s:
|
|
word_1[char] += 1
|
|
for char in t:
|
|
word_2[char] += 1
|
|
for char, count in word_2.items():
|
|
"""
|
|
{
|
|
"n": 1,
|
|
"a": 3,
|
|
"g": 1,
|
|
"r": 1
|
|
}
|
|
"""
|
|
if not word_1.get(char) == count:
|
|
return False
|
|
return True
|
|
|
|
|
|
sc = Solution()
|
|
|
|
s = "anagram"
|
|
t = "nagaram"
|
|
result = True
|
|
solution = sc.isAnagram(s, t)
|
|
assert solution == result, f"Solution: {solution}, result: {result}"
|
|
|
|
s = "rat"
|
|
t = "car"
|
|
result = False
|
|
solution = sc.isAnagram(s, t)
|
|
assert solution == result, f"Solution: {solution}, result: {result}"
|
|
|
|
s = "ab"
|
|
t = "b"
|
|
result = False
|
|
solution = sc.isAnagram(s, t)
|
|
assert solution == result, f"Solution: {solution}, result: {result}"
|
|
|