Leetcode 30.Substring with concatenation of all Words (connected to all substrings in Words) ideas and methods for solving problems

Source: Internet
Author: User

Substring with concatenation of all Words


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).


Ideas: Leetcode on some problems pass rate is low, and not necessarily is the algorithm difficult, I think a large part of the reason is the problem description is not clear, resulting in the rule understanding is not thorough, so the programming time will occur the rule understanding deviation.

The subject is the same, just beginning to the rule understanding deviation more, thought wors in the substring appears once can, regardless of repetition or not repeating, but later submitted but, see case completely understand the wrong rule, can only rewrite code, very troublesome.

Complaints are large, and rules and regulations are important.

The code is as follows:

public class Solution {public list<integer> findsubstring (String s, string[] words) {list<integer>        List = new arraylist<integer> ();        if (Words.length = = 0 | | s.length () = = 0) {return list; } map<string,integer> Map = new hashmap<string,integer> ();//save number and value for (int i = 0; i < words. Length i++) {if (Map.get (words[i]) = = null) {map.put (words[i],1);//Save Word}else{m  Ap.put (Words[i],map.get (words[i]) +1);//The value saved by Word +1}} map<string,integer> Mapvalue = new Hashmap<string,integer> (map)///Save the number of repetitions to facilitate re-assignment int len = words.length;//The array length after the duplicate is removed int wordle        n = words[0].length ();        String temp = "";            int count = 0;//Each word appears once, recording for (int i = 0; I <= s.length ()-len*wordlen;i++) {count = 0;//initialization for (int j = 0; J < len;j++) {temp = s.substring (i + j * Wordlen, i + (j+1) * wordlen);//intercept Wordlen long string if (Map.get (temp)! = null && map.get (temp)! = 0) {//If map has more than 0                    A map.put (Temp,map.get (temp)-1),//map value minus 1 count++;//records +1}else{                Break            }} if (count = = len) {//If the number of records is equal to Len, then the requirement list.add (i) is met;             }//hashmap re-initializes the for (String Key:map.keySet ()) {//Such a faster map.put (Key,mapvalue.get (key));    }} return list; }}


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

Leetcode 30.Substring with concatenation of all Words (connected to all substrings in Words) ideas and methods for solving problems

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.