438. Find All Anagrams in a String
題目網址:https://leetcode.cn/problems/find-all-anagrams-in-a-string/
題意:給兩 string
s
和p
, 找到s
中所有p
的異位詞, 並返回這些 substring 的起始 index。異位詞:由相同字母重排列形成的 string(包括相同的 string)
Solution:
想法:利用 Sliding Window, 同 567. Permutation in String
class Solution { |
- time:$O(n1 + n2)$ ➔
n1
、n2
分別為s
、p
的長度- $O(n1)$:
s
中的每個元素最多被遍歷 2 次(left
、right
) - $O(n2)$:遍歷
p
計算need
- $O(n1)$:
- space:$O(1)$ ➔
window
、need
長度皆為 $O(26)$
本部落格所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議。轉載請註明來自 Zako's Blog!
評論