problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::find_all_anagrams_in_a_string::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< int > findAnagrams (string s, const string &p)
 

详细描述

在文件 leetcode.h2332 行定义.

成员函数说明

◆ findAnagrams()

vector< int > leetcode::find_all_anagrams_in_a_string::Solution::findAnagrams ( string  s,
const string &  p 
)
static

在文件 leetcode.cpp6163 行定义.

6163 {
6164 vector<int> ans;
6165 unordered_map<char, int> um_p;
6166 for(char ch: p) {
6167 um_p[ch]++;
6168 }
6169 unordered_map<char, int> um_s;
6170 for(int i = 0; i < p.length(); i++) {
6171 um_s[s[i]]++;
6172 }
6173 if(um_s == um_p) {
6174 ans.emplace_back(0);
6175 }
6176 for(int i = p.length(); i < s.length(); i++) {
6177 um_s[s[i]]++;
6178 um_s[s[i - p.length()]]--;
6179 if(um_s[s[i - p.length()]] == 0) {
6180 um_s.erase(s[i - p.length()]);
6181 }
6182 if(um_s == um_p) {
6183 ans.emplace_back(i - p.length() + 1);
6184 }
6185 }
6186 return ans;
6187 }

被这些函数引用 leetcode::find_all_anagrams_in_a_string::TEST().


该类的文档由以下文件生成: