๐Ÿ“ฆ schubart / LeetCode

๐Ÿ“„ lc_0242.py ยท 26 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26from collections import Counter


# noinspection PyMethodMayBeStatic,PyPep8Naming
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:

        # Certainly not an anagram if lengths differ.
        if len(s) != len(t):
            return False

        # Build histogram of characters in s.
        frequencies = Counter(s)

        # Discount frequency of every character in t.
        for c in t:
            frequencies[c] -= 1

            # Fail if c was in t more often than in s.
            if frequencies[c] < 0:
                return False

        # Since both strings are same length and there are
        # no excess characters in t, strings must be anagrams.
        return True