Java [Leetcode 30]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 onc E and without any intervening characters.

For example, given:
s:"barfoothefoobarman"
words:["foo", "bar"]

You should return the indices: [0,9] .
(Order does not matter).

Problem Solving Ideas:

First set a hashmap, inside the key value is a word, the element value is the number of occurrences of the corresponding word, as a comparison of the template.

We first get the first word in the first position of the string in a large loop, and see if the word exists in the template map above. If it does not exist, then the outside cycle directly back one, matching the word at the beginning of the next position. Inline loops are also used to troubleshoot words by word. If the lookup is present, then we add the word to the new map, and the number of statistics also needs to be incremented. Next, see if the number of occurrences in this new map is less than the number of occurrences of that word in the template, if it is greater than the number of times, it indicates that the condition is non-conforming, jumps out of the inner loop, and the outer loop pointer moves back.

Finally, if the inner loop runs smoothly, it means that all the words are matched at the beginning of the position, then add the position to the list, otherwise the outer loop pointer points to the next character's external, and continues to start a similar judgment.

The code is as follows:

public class Solution {public list<integer> findsubstring (String s, string[] words) {list<integer> List = New Arraylist<integer> (); map<string, integer> map = new hashmap<string, integer> (); map<string, integer> tmp = new hashmap<string, integer> (); int slength = S.length (); int wordsnum = Words.length int wordslength = Words[0].length (); int j;if (Slength < Wordsnum | | wordsnum = 0) return list;for (int i = 0; i < W Ordsnum; i++) {if (Map.containskey (Words[i])) Map.put (Words[i], Map.get (words[i]) + 1); Elsemap.put (Words[i], 1);}  for (int i = 0, i <= slength-wordsnum * wordslength; i++) {tmp.clear (); for (j = 0; J < Wordsnum; J + +) {String word = S.substring (i + j * wordslength, i + j* wordslength + wordslength); if (!map.containskey (word)) break;if (Tmp.containskey (word)) Tmp.put (Word, tmp.get (word) + 1), Elsetmp.put (Word, 1), if (Tmp.get (word) > Map.get (Word)) break;} if (j = = Wordsnum) list.add (i);} return list;}}

Java [Leetcode 30]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.