Substring with Concatenation of All Words

来源:互联网 发布:宜搜小说软件下载 编辑:程序博客网 时间:2024/06/08 16:04

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 words exactly 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).
class Solution {public:    vector<int> findsubstring(string s, vector<string>& words) {        vector<int> result;        if(s.empty() || words.empty() || words.size() * words[0].size() > s.size())            return result;        unordered_map<string, int> target_map;        for(auto &word : words)            ++target_map[word];        int num_words = words.size();        int char_word = words[0].size();        //遍历源字符串查找而不是目标数组        for(int index = 0; index <= s.size() - num_words * char_word; index++ )        {            unordered_map<string, int>target_exit;            int j;            for(j = 0; j < num_words; j++)            {                string target = s.substr(index + j * num_words, num_words);                if(target_map.find(target) == target_map.end())                    break;                ++target_exit[target];                if(target_map[target] < target_exit[target])                     break;            }            if(j == num_words)            {                cout << "tset" << endl;                result.push_back(index);            }        }        return result;    } };

官方给出的O(n)算法

vector<int> findSubstring(string S, vector<string> &L) {        vector<int> ans;        int n = S.size(), cnt = L.size();        if (n <= 0 || cnt <= 0) return ans;        // init word occurence        unordered_map<string, int> dict;        for (int i = 0; i < cnt; ++i) dict[L[i]]++;        // travel all sub string combinations        int wl = L[0].size();        for (int i = 0; i < wl; ++i) {            int left = i, count = 0;            unordered_map<string, int> tdict;            for (int j = i; j <= n - wl; j += wl) {                string str = S.substr(j, wl);                // a valid word, accumulate results                if (dict.count(str)) {                    tdict[str]++;                    if (tdict[str] <= dict[str])                         count++;                    else {                        // a more word, advance the window left side possiablly                        while (tdict[str] > dict[str]) {                            string str1 = S.substr(left, wl);                            tdict[str1]--;                            if (tdict[str1] < dict[str1]) count--;                            left += wl;                        }                    }                    // come to a result                    if (count == cnt) {                        ans.push_back(left);                        // advance one word                        tdict[S.substr(left, wl)]--;                        count--;                        left += wl;                    }                }                // not a valid word, reset all vars                else {                    tdict.clear();                    count = 0;                    left = j + wl;                }            }        }        return ans;    }
0 0
原创粉丝点击