当前位置:   article > 正文

LeetCode题解——Substring with Concatenation of All Words_given a list of words l, that are all the same len

given a list of words l, that are all the same length,andg a string, s, find

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters.

For example, given:
s"barfoothefoobarman"
words["foo", "bar"]

You should return the indices: [0,9].

(order does not matter).

解题思路:

(1)  怎样得到所有候选解?

一种方法是用大小为words.size()*words[0].size()的window在s上滑动选取候选集,即得到的候选集为:barfoo,arfoot,rfooth... 等等.然后对候选解处理,判断是否是一个concatenation。

另一种方法的思想是:通过words[0].size()次移位覆盖所有候选集,具体思想是:

方法二:引入变量left,假设left = 0,即从第0个元素起开始处理,遍历每个单词,形式为:bar foo the foo bar man; left = 1时,遍历每次单词,为:arf oot hef oob arm; left=2时,遍历每个单词,形式为:rfo oth efo oba rma. 当left>=word[0].size()= 3时,单词的划分形式和left=1相同。因此,上述三种划分覆盖了所有的单词划分形式。而每个候选解即是这些单词的顺序组合。


(2)  怎样判断候选解?

对words建立一个词典,对s建立一个词典,比较s的词典和words中的词典是否吻合,若是,则找到一个符合条件的解。

方法二代码:

  1. class Solution {
  2. public:
  3. // travel all the words combinations to maintain a window
  4. // there are wl(word len) times travel
  5. // each time, n/wl words, mostly 2 times travel for each word
  6. // one left side of the window, the other right side of the window
  7. // so, time complexity O(wl * 2 * N/wl) = O(2N)
  8. vector<int> findSubstring(string S, vector<string> &L) {
  9. vector<int> ans;
  10. int n = S.size(), cnt = L.size();
  11. if (n <= 0 || cnt <= 0) return ans;
  12. // init word occurence
  13. unordered_map<string, int> dict;
  14. for (int i = 0; i < cnt; ++i) dict[L[i]]++;
  15. // travel all sub string combinations
  16. int wl = L[0].size();
  17. for (int i = 0; i < wl; ++i) {
  18. int left = i, count = 0;
  19. unordered_map<string, int> tdict;
  20. for (int j = i; j <= n - wl; j += wl) {
  21. string str = S.substr(j, wl);
  22. // a valid word, accumulate results
  23. if (dict.count(str)) {
  24. tdict[str]++;
  25. if (tdict[str] <= dict[str])
  26. count++;
  27. else {
  28. // a more word, advance the window left side possiablly
  29. while (tdict[str] > dict[str]) {
  30. string str1 = S.substr(left, wl);
  31. tdict[str1]--;
  32. if (tdict[str1] < dict[str1]) count--;
  33. left += wl;
  34. }
  35. }
  36. // come to a result
  37. if (count == cnt) {
  38. ans.push_back(left);
  39. // advance one word
  40. tdict[S.substr(left, wl)]--;
  41. count--;
  42. left += wl;
  43. }
  44. }
  45. // not a valid word, reset all vars
  46. else {
  47. tdict.clear();
  48. count = 0;
  49. left = j + wl;
  50. }
  51. }
  52. }
  53. return ans;
  54. }
  55. };

方法一代码:

  1. class Solution {
  2. public:
  3. vector<int> findSubstring(string s, vector<string>& words) {
  4. vector<int> ans;
  5. int sz = s.size();
  6. int n = words.size();
  7. if(!sz || !n) return ans;
  8. int l = words[0].size();
  9. int t_size = n*l;
  10. unordered_map<string,int> t_map;
  11. for(auto c:words){
  12. t_map[c]++;
  13. }
  14. int start = 0;
  15. while(start+t_size<=sz){
  16. unordered_map<string,int> s_map;
  17. int count=0;
  18. for(int i=start;i<t_size+start;i+=l){
  19. string c = s.substr(i,l);
  20. if(t_map[c]>0){
  21. ++s_map[c];
  22. if(s_map[c]==t_map[c]) count+=t_map[c];
  23. else if(s_map[c]>t_map[c]) break;
  24. }
  25. else break;
  26. }
  27. if(count==n) ans.push_back(start);
  28. start++;
  29. }
  30. return ans;
  31. }
  32. };




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/252622
推荐阅读
相关标签
  

闽ICP备14008679号