# [Leetcode] 030. Substring with concatenation of any Words (hard) (C++/java)

Source: Internet
Author: User

Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode

030. Substring with concatenation of all Words (hard) links

Title: https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/
Code (GitHub): Https://github.com/illuz/leetcode

Test Instructions

Give a string s and a word list, the word length is the same, find all the substrings of S, the substring consists of all the words, and returns the starting position of the substring.

Analysis

It is obvious that each substring is made up of all the words, and the length is certain, so it is OK to enumerate the substrings directly, and then use the map to judge the strings.

This algorithm complexity is O (n*m), in fact there are several cool O (n) algorithm:

1. With "076. Minimum window Substring (hard) "The same idea, is to maintain the two pointers, respectively, and a map, run the front of the pointer to see if the current interval can match exactly, then get the answer."
2. There is a magic `map<string, queue>` to do, in fact, the principle is the same as 1, specifically see: Code with the HashTable with the Queue for O (n) runtime
3. This is actually just an optimization, using Trie matching when matching, see: Accepted recursive solution using Trie Tree

This implements the O (n*m) algorithm in C + + and implements the 1 algorithm in Java.

Code

C++:

`Class Solution {public:    vector<int> findsubstring (String S, vector<string> &l) {map<string, int > words;map<string, int> curwords;vector<int> ret;int slen = s.length (); if (!slen | | L.empty ()) return ret;int Llen = L.size (), Wlen = L[0].length ();//Record The current words mapfor (auto &i:l) ++word s[i];//check the [Llen * Wlen] substringfor (int i = 0; i + llen * wlen <= slen; i++) {curwords.clear (); int j = 0;//C Heck the wordsfor (j = 0; J < Llen, J + +) {string tmp = S.SUBSTR (i + j * Wlen, Wlen); if (Words.find (tmp) = = Words.end ()) Break;++curwords[tmp];if (Curwords[tmp] > Words[tmp]) break; if (j = = Llen) ret.push_back (i);} return ret;    }};`

Java:

`public class Solution {public list<integer> findsubstring (String S, string[] L) {list<integer> ret        = new Arraylist<integer> ();        int slen = S.length (), Llen = L.length;        if (slen <= 0 | | Llen <= 0) return ret;        int wlen = L[0].length ();        Get the words ' map hashmap<string, integer> words = new hashmap<string, integer> ();            for (String str:l) {if (Words.containskey (str)) {words.put (str, words.get (str) + 1);            } else {words.put (str, 1);            }} for (int i = 0; i < Wlen; ++i) {int left = i, Count = 0;            hashmap<string, integer> tmap = new hashmap<string, integer> ();                for (int j = i; J <= Slen-wlen; j + = Wlen) {String str = s.substring (J, J + Wlen);               if (Words.containskey (str)) {if (Tmap.containskey (str)) {         Tmap.put (str, tmap.get (str) + 1);                    } else {tmap.put (str, 1);                    } if (Tmap.get (str) <= words.get (str)) {count++; } else {//Too many words, push the ' left ' forward while (Tmap.get (str) ;                            Words.get (str)) {String tmps = s.substring (left, left + Wlen);                            Tmap.put (Tmps, Tmap.get (tmps)-1);                                if (Tmap.get (Tmps) < Words.get (tmps)) {//if affect the count                            count--;                        } left + = Wlen;                        }}//Get the answer if (count = = Llen) {                        Ret.add (left);                        It ' s better to push forward onceString tmps = S.substring (left, left + Wlen);                        Tmap.put (Tmps, Tmap.get (tmps)-1);                        count--;                    Left + = Wlen;                    }} else {//not any match word tmap.clear ();                    Count = 0;                left = j + Wlen;    }}} return ret; }}`

[Leetcode] 030. Substring with concatenation of any Words (hard) (C++/java)

Related Keywords:
Related Article

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.