给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。
s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。
例如,如果 words = [“ab”,“cd”,“ef”], 那么 “abcdef”, “abefcd”,“cdabef”, “cdefab”,“efabcd”, 和 “efcdab” 都是串联子串。 “acdbef” 不是串联子串,因为他不是任何 words 排列的连接。
返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。
C++
class Solution {
public:
string nstring(string str,int n){
string result_str="";
for( int i=0;i<n;i++ ){
result_str.append(str);
}
return result_str;
}
unordered_map<string,int> list2map(vector<string>& temp_vector){
unordered_map<string,int> result_map;
for( string temp_str:temp_vector ){
result_map[temp_str]++;
}
return result_map;
}
void refillmap(unordered_map<string,int>& result_map,unordered_map<string,int>& data_map){
for( const auto& pair : result_map ){
data_map[pair.first]=pair.second;
}
}
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
int length = words[0].size();//the length of the word.
int total_length=length*words.size();//the total length of the word.
if( total_length>s.size() ){
return res;
}
unordered_map<string,int> result_map=list2map(words);
unordered_map<string,int> data_map;
refillmap(result_map,data_map);
string target_str="";
if(1==data_map.size()){
target_str=nstring(words[0],words.size());
}
int gap=length;
for( int i=0;i<s.size();i++ ){
if( s.size()-i<total_length){
break;
}
int n=words.size();
if(1==data_map.size()){
gap=total_length;
string current_str=s.substr(i,gap);
if(current_str==target_str){
re

最低0.47元/天 解锁文章
556

被折叠的 条评论
为什么被折叠?



