【Leetcode】python - [242] Valid Anagram 個人解法筆記 (updated: 2022/4/28)

題目出處

242. Valid Anagram

難度

easy

題目分類

Hash table, string, sorting

個人範例程式碼 - 2022/4/28 (與 2022/3/4 解法完全相同)

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        return Counter(s) == Counter(t)

Time Complexity

O(n)

Space Complexity

x

算法說明

這題目碰到 python Counter,一行再見… python 太強大了。

這題就是計算數量,看有沒有相等,相等就是 True,
而 python collection 內建的 Counter function,
快速幫我們把計算數量的 hash dictionary 完成了。

如果還不知道 python Counter 用法的,「強烈建議」一定要會,寫程式效率會快超級多!!!
可參考我的另外一篇文:
【Python】python counter() 用法整理 – 快速計算資料內容的數量

我們就不用再另外寫 hash table 做 count 的動作了。

corner case 特殊情況處理

x

Boundary conditions/ Edge conditions 邊際情況處理

x

Reference