抖音直播间挂人气自助网站,做新网站推广的活动,app拉新推广,期刊网站建设题目链接#xff1a;leetcode 30
1.题目
给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。
s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。
例如#xff0c;如果 words [“ab”,“cd”,“ef”]#xff0c; …题目链接leetcode 30
1.题目
给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。
s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。
例如如果 words [“ab”,“cd”,“ef”] 那么 “abcdef” “abefcd”“cdabef” “cdefab”“efabcd” 和 “efcdab” 都是串联子串。 “acdbef” 不是串联子串因为他不是任何 words 排列的连接。 返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。
2.示例
1示例 1 输入s “barfoothefoobarman”, words [“foo”,“bar”] 输出[0,9] 解释因为 words.length 2 同时 words[i].length 3连接的子字符串的长度必须为 6。 子串 “barfoo” 开始位置是 0。它是 words 中以 [“bar”,“foo”] 顺序排列的连接。 子串 “foobar” 开始位置是 9。它是 words 中以 [“foo”,“bar”] 顺序排列的连接。 输出顺序无关紧要。返回 [9,0] 也是可以的。
2示例 2 输入s “wordgoodgoodgoodbestword”, words [“word”,“good”,“best”,“word”] 输出[] 解释因为 words.length 4 并且 words[i].length 4所以串联子串的长度必须为 16。 s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。 所以我们返回一个空数组。
3示例 3 输入s “barfoofoobarthefoobarman”, words [“bar”,“foo”,“the”] 输出[6,9,12] 解释因为 words.length 3 并且 words[i].length 3所以串联子串的长度必须为 9。 子串 “foobarthe” 开始位置是 6。它是 words 中以 [“foo”,“bar”,“the”] 顺序排列的连接。 子串 “barthefoo” 开始位置是 9。它是 words 中以 [“bar”,“the”,“foo”] 顺序排列的连接。 子串 “thefoobar” 开始位置是 12。它是 words 中以 [“the”,“foo”,“bar”] 顺序排列的连接。
3.分析
首先我们可以明确假设某个串联子串在s的开始位置为pos那么有如下结论 1这个串联子串的长度为words[0].size()*words.size() 2以words[0].size()对这个子串进行划分每个单词应该存在于words中且在words中出现的次数和在子串中出现的次数保持一致 为了确定每个划分的单词是否和words中的单词匹配我们预先处理确定对于0is.size(),以i位置开头且长度为words[0].size()的单词和words中的哪个单词是匹配的 在此基础上我们可以预先确定一个最暴力的思想那么就是枚举s中的每个位置作为子串的开始看是否满足条件我们在此基础上引入滑动窗口的思想。对于i开头的子串和对于iwords[0].size()开始的子串他们不相同的只有[i,iwords.size()-1]以及[iwords[0].size()*words.size()-1,(iwords[0].size()*words.size()-1)words.size()-1]这两个部分那么可以只更改这两部分对结果造成的影响即可
4.代码
class Solution {
public:int get_id(string s,vectorstring words,int st,int num){if(st0) return -1;string s_tests.substr(st,num);for(int i0;iwords.size();i)if(s_testwords[i]) return i;return -1;}mapint,int map2;mapstring,int map3;vectorstring words;void get_map2(string s, vectorstring words){for(int i0;is.size();i)map2[i]-1;for(int i0;iwords[0].size()-1s.size();i)for(int j0;jwords.size();j){string s1s.substr(i,words[0].size());if(s1words[j]) map2[i]j;}}void get_map3(string s, vectorstring words2){sort(words2.begin(),words2.end());for(int i0;iwords2.size();i)map3[words2[i]]0;for(int i0;iwords2.size();i){map3[words2[i]];if(i0){words.push_back(words2[i]);continue;}if(words2[i]!words2[i-1])words.push_back(words2[i]);}}vectorint findSubstring(string s, vectorstring words2) {int words_lengthwords2[0].size();int sum_lengthwords2.size()*words2[0].size();get_map3(s,words2);get_map2(s,words);vectorint ans;for(int i0;iwords_length;i){mapint,int map1;for(int j-1;jwords.size();j)map1[j]0;for(int ji;jsum_length-1s.size();jwords_length){bool flagtrue;int st1j-words_length;int st2jsum_length-words_length; if(ji){for(int kj;kst2;kwords_length)map1[map2[k]];}else{map1[map2[st2]];map1[map2[st1]]--;}for(int k0;kwords.size();k)if(map1[k]!map3[words[k]]) {flagfalse;}if(flagtrue) ans.push_back(j);}}return ans;}
};