ubstring with Concatenation of All Words

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.

汗颜!!!

英语不好,这题看半天才懂题目的意思:在字符串S里找一些子串,将这些子串的下标索引返回到数组中保存。

     那么子串要满足什么条件呢?  子串是由数组words里的所有单词乱序填充的,所有单词仅出现一次。

           注意找到所有符合条件的子串!!!!!

  看懂题目后应该就不难了。注意-----数组words的单词应该可能有重复的。

 1 class Solution {
 2 public:
 3     vector<int> findSubstring(string s, vector<string>& words) {
 4       vector<int> res;
 5       map<string,int> all;
 6       map<string,int> cur;
 7       int nums=words.size();
 8       for(int s=0;s<nums;s++)
 9           all[words[s]]++;
10       int len=words[0].size();
11       if(s.size()<len*nums) return res;
12       for(int i=0;i<=s.size()-len*nums;i++)
13        {
14            cur.clear();
15            int j;
16            for( j=0;j<nums;j++)
17            {
18                string subs=s.substr(i+j*len,len);
19                if(all.find(subs)==all.end()) break;
20                cur[subs]++;
21                if(cur[subs]>all[subs])break;
22            }
23            if(j==nums) res.push_back(i);
24        }
25        return res;
26     }
27 };
原文地址:https://www.cnblogs.com/daocaorenblog/p/5266164.html