标签:顺序 containe mono oat tle 分享 one cte amp
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | class Solution { public : vector< int > findSubstring(string s, vector<string>& words) { int N = s.size(); int M = words[0].size(); int L = words.size(); int last = N - M + 1; vector< int > result; unordered_map<string, int > mapping; vector<vector< int >> table(2,vector< int > (L)); vector< int > smapping(last, -1); int index = 0, failures = 0; for ( int i = 0; i < L; ++i) { auto word = mapping.find(words[i]); if (word == mapping.end()) { failures++; mapping.insert({ words[i], index++ }); word = mapping.find(words[i]); } table[0][word->second]++; } for ( int i = 0; i < last; ++i) { auto word = mapping.find(s.substr(i, M)); if (word != mapping.end()) { smapping[i] = word->second; } else { smapping[i] = -1; } } for ( int i = 0; i < M; ++i) { int currentFailures = failures; fill(table[1].begin(), table[1].end(), 0); int L = i, R = i; while (R < last) { while (currentFailures != 0 && R < last) { if (smapping[R] != -1 && ++table[1][smapping[R]] == table[0][smapping[R]]) { currentFailures--; } R += M; } while (currentFailures == 0 && L < R) { if ((R - L) / M == words.size()) { result.push_back(L); } if (smapping[L] != -1 && --table[1][smapping[L]] == table[0][smapping[L]] - 1) { currentFailures++; } L += M; } } } return result; } }; |
30. Substring with Concatenation of All Words
标签:顺序 containe mono oat tle 分享 one cte amp
原文地址:http://www.cnblogs.com/zhxshseu/p/5df501ce21791b3c3cf3229bb03f5b23.html