Topic Connection
https://leetcode.com/problems/substring-with-concatenation-of-all-words/
Substring with concatenation of all wordsdescription
You is given a string, s, and a list of words, words, that is all of the same length. Find all starting indices of substring (s) in S, a concatenation of each word in wordsexactly once and without any I ntervening characters.
For example, given:
S"barfoothefoobarman"
Words["foo", "bar"]
You should return the indices: [0,9] .
(Order does not matter).
According to test instructions Direct simulation (write very slow run Qaq)
Class Solution {public: vector<int> findsubstring (string s, vector<string>& words) { vector <int> Res; if (S.empty () | | words.empty ()) return res; for (auto &r:words) a[r]++; int n = words[0].size (), M = S.size (), k = Words.size (); int len = m-n * k + 1; for (int i = 0; i < len; i++) { int d = k, j = i; B.clear (); while (d) { string temp = S.substr (j, N); J + = N; if (a.find (temp) = = A.end ()) {break ; } else { b[temp]++; if (B[temp] > A[temp]) break; d--; } } if (!d) res.push_back (i); } return res; } Private: map<string, int> A, B;};
Leetcode Substring with concatenation of all Words