---
# System prepended metadata

title: 438. Find All Anagrams in a String
tags: [Leetcode]

---

<style>
    html, body, .ui-content {
    background: #222222;
    color: #00BFFF;
}
    /* 設定 code 模板 */
    .markdown-body code,
.markdown-body tt {
    background-color: #ffffff36;
}
.markdown-body .highlight pre,
.markdown-body pre {
    color: #ddd;
    background-color: #00000036;
}
.hljs-tag {
    color: #ddd;
}
.token.operator {
  background-color: transparent;
}    
/* 設定連結 */
a,
.open-files-container li.selected a {
    color: #89FFF8;
    
}
a:hover,
.open-files-container li.selected a:hover {
    color: #89FFF890;
}
   
</style>

###### tags: `Leetcode`

# 438. Find All Anagrams in a String


###### Link : https://leetcode.com/problems/find-all-anagrams-in-a-string/description/

## 題目
找到s中所有p的排列組合的開頭
## 程式碼
```cpp=
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        const int pSize = p.size(), sSize = s.size();
        if(sSize < pSize) return vector<int>();
        vector<int> flag(26), temp(26), ans;
        for(int i = 0;i < pSize;i++){
            temp[s[i] - 'a']++;
            flag[p[i] - 'a']++;
        }
        for(int i = pSize;i < sSize;i++){
            if(temp == flag)
                ans.push_back(i - pSize);
            temp[s[i] - 'a']++;
            temp[s[i - pSize] - 'a']--;
        }
        if(temp == flag)
            ans.push_back(sSize - pSize);
        return ans;
    }
};
```

## Date 
### 2023/2/5