LeetCode: Substring with Concatenation of All Words (summarize)

Source: Internet
Author: User

You are given a string, S, and a list of words, L, that are all of the same length. find all starting indices of substring (s) in S that is a concatenation of each word in L exactly once and without any intervening characters. for example, given: S: "barfoothefoobarman" L: ["foo", "bar"] You shoshould return the indices: []. (order does not matter ). algorithm 1: brute-force solution, which is used to determine each position of string s once (if the length of the substring starting from the current position is less than the length of all words in L, You don't need to judge). Can the first part of the substring starting from the current position be concatenated by the words in the set L. When judging from a position I, the words s [I, I + 2], s [I + 3, I + 5], s [I + 6, I + 8]… Whether the word is in the collection. If the word is in the collection, the word is deleted from the collection. We use a hash map to save words, so that we can determine whether the time complexity of the word in the set is O (n * (l * k) in O (1) time )) n is the length of a string, l is the number of words, and k is the word length. Recursive code is as follows: class Solution {private: int wordLen; public: vector <int> findSubstring (string S, vector <string> & L) {unordered_map <string, int> wordTimes; for (int I = 0; I <L. size (); I ++) if (wordTimes. count (L [I]) = 0) wordTimes. insert (make_pair (L [I], 1); else wordTimes [L [I] ++; wordLen = L [0]. size (); vector <int> res; For (int I = 0; I <= (int) (S. size ()-L. size () * wordLen); I ++) if (helper (S, I, wordTimes, L. size () res. push_back (I); return res;} // judge the substring s [index...] can the front-end of bool helper (string & s, const int index, unordered_map <string, int> & wordTimes, const int wordNum) be combined by words in L) {if (wordNum = 0) return true; string firstWord = s. substr (index, wordLen); unordered_map <string, int >:: iterator ite = wordTimes. find (first Word); if (ite! = WordTimes. end () & ite-> second> 0) {(ite-> second) --; bool res = helper (s, index + wordLen, wordTimes, wordNum-1 ); (ite-> second) ++; // restore the return res status of hash map;} else return false ;}}; non-recursive code: class Solution {private: int wordLen; public: vector <int> findSubstring (string S, vector <string> & L) {unordered_map <string, int> wordTimes; for (int I = 0; I <L. size (); I ++) if (wordTimes. count (L [I]) = 0) wo RdTimes. insert (make_pair (L [I], 1); else wordTimes [L [I] ++; wordLen = L [0]. size (); vector <int> res; for (int I = 0; I <= (int) (S. size ()-L. size () * wordLen); I ++) if (helper (S, I, wordTimes, L. size () res. push_back (I); return res;} // judge the substring s [index...] can the front-end of the for-bool helper (const string & s, int index, unordered_map <string, int> wordTimes, int wordNum) {for (int I = index; wordNum! = 0 & I <= (int) s. size ()-wordLen; I + = wordLen) {string word = s. substr (I, wordLen); unordered_map <string, int >:: iterator ite = wordTimes. find (word); if (ite! = WordTimes. end () & ite-> second> 0) {ite-> second --; wordNum --;} else return false;} if (wordNum = 0) return true; else return false ;}; OJ recursion time is less than non-recursive time, because in non-recursive helper functions, the hash map parameter is used to pass values, each call needs to copy a hash map. There is always only one hash map object algorithm in the recursive code. 2. Recall the previous questions: LeetCode: Longest Substring Without Repeating Characters and LeetCode: Minimum Window Substring, A sliding window method is used. The same idea can also be used for this question. For example, the window s = "a1b2c3a1d4" L = {"a1", "b2", "c3", and "d4"} is empty at the beginning, and a1 is in L, add window a1 b2c3a1d4 address b2 in L, add window a1b2 c3a1d4 c3 in L, add window a1b2c3 a1d4 a1 in L, however, the previous a1 has been calculated once. In this case, you only need to move the a1 [b2c3a1] d4 d4 to the right of the window in L, add a1 [b2c3a1d4] to the window and find a match. If you change s to "a1b2c3kka1d4", you will encounter the word kk in Step 4, And kk is not in L, at this time, the start position of the window is moved to the end of a1b2c3kk [a1d4 class Solution {public: vector <int> findSubstring (string S, vector <string> & L) {unordered_map <string, int> wordTimes; // L Number of times a word appears for (int I = 0; I <L. size (); I ++) if (wordTimes. count (L [I]) = 0) wordTimes. insert (make_pair (L [I], 1); else wordTimes [L [I] ++; int wordLen = L [0]. size (); vector <int> res; for (int I = 0; I <wordLen; I ++) {// in order not to miss the substring starting from every position of s, the first loop is the length of the word unordered_map <string, int> wordTimes2; // number of times words appear in the current window int winStart = I, cnt = 0; // winStart is the start position of the window, cnt is the number of words in the current window for (int winEnd = I; winEnd <= (int) S. size () -WordLen; winEnd + = wordLen) {// The window is [winStart, winEnd) string word = S. substr (winEnd, wordLen); if (wordTimes. find (word )! = WordTimes. end () {if (wordTimes2.find (word) = wordTimes2.end () wordTimes2 [word] = 1; else wordTimes2 [word] ++; if (wordTimes2 [word] <= wordTimes [word]) cnt ++; else {// The current word is in L, but it has displayed the corresponding number of times in the window, do not add a window // at this time, you should move the start position of the window to the left, and the next word position of the first appearance of the word for (int k = winStart; k ++ = wordLen) {string tmpstr = S. substr (k, wordLen); wordTimes2 [tmpstr] --; if (tmpstr = word) {winStart = k + wordLen; break;} cnt --;}} if (cnt = L. size () res. push_back (winStart);} else {// find the word winStart = winEnd + wordLen; wordTimes2.clear (); cnt = 0 ;}} return res ;}} that is not in L ;}}; the algorithm time complexity is O (n * k). n is the length of a string, and k is the length of a word.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.