Leetcode--Substring with concatenation of all Words

Source: Internet
Author: User

Title Description:
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 that's 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).


The title means that given 1 strings s and a word array words, the number is n, and the length of each word is L.
Scan S, judging within the L * N range, determine if each word in words appears only once in S [I, l*n] (if words contains 1 words multiple times, where I∈[0,len (s)-l*n)
Note that if the words itself contains duplicate words, then this word also allows the same number of occurrences in S[i, L*n]




Ideas:
1. Use the hash table 1:hash, traverse the word array, save all the words and the number of occurrences
2. Using hash table 2:HASH2, intercept the string str of length L in S, if Str appears in Hash1, add to Hash2, where l=s[i, Words[j] * l]
3. Each time you determine whether the element in the HASH1 is exactly equal to HASH2


Implementation code:






public class Solution {public    ilist<int> findsubstring (string s, string[] words) {           if (string. Isnullorwhitespace (s) | | Words = = NULL | | S.length < words. Length * Words[0]. Length) {return new list<int> ();} var Wlen = words[0]. Length;var result = new list<int> (); var len = words. Length * Wlen;var hash = new dictionary<string, int> (); for (var i = 0;i < words. Length; i++) {if (!hash. ContainsKey (Words[i])) {hash. ADD (Words[i], 1);} Else{hash[words[i]] + +;}} var h = new dictionary<string, int> (), for (var i = 0; i < S.length-len + 1; i++) {for (var j = 0;j < words. Length; J + +) {var str = s.substring (i + j * Wlen, Wlen), if (hash. ContainsKey (str)) {if (!h.containskey (str)) {h.add (str,1);} ELSE{H[STR] + +;}}} if (hash. All (X=>h.containskey (X.key) && h[x.key] = = X.value)) {result. ADD (i);} var k2 = new list<string> (H.keys); foreach (var k in K2) {h[k] = 0;}} return result;}    }


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode--Substring with concatenation of all Words

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.