150強訪談簡單

有效的字謎詞

「有效字謎」問題的詳細指南和 Python 實作。

問題陳述

簡單

給定兩個字串 st,如果 ts 的字謎,則傳回 True,否則傳回 False

字謎詞是透過使用所有原始字母一次重新排列不同單字或短語的字母而形成的單字或短語。

寫一個函數 isAnagram(s: str, t: str) -> bool

約束條件
  • 1 <= len(s), len(t) <= 5 * 10^4
  • s and t consist of lowercase English letters

範例

Example 1
Input
s = "anagram", t = "nagaram"
Output
True
Explanation

Both strings contain the same characters with the same frequencies: a(3), n(1), g(1), r(1), m(1).

Example 2
Input
s = "rat", t = "car"
Output
False
Explanation

'rat' contains 't' but 'car' does not. They have different character compositions.

Need a Hint?
考慮使用數組和哈希特定的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。