"Leetcode-Interview algorithm classic-java Implementation" "030-substring with concatenation of all Words (concatenation of all words in a substring)"

Source: Internet
Author: User

"030-substring with concatenation of all Words (concatenation of all words in a substring)" "leetcode-Interview algorithm classic-java Implementation" "All Topics folder Index" Original Question

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

Main Topic

Given a string s and a string array, the length of the string in Words,wrods is equal. Find out that all the substrings in s are exactly the same as all the characters in the words, returning the starting position of the substring.


Thinking of solving problems

Turn the words into a hashmap

Code Implementation

Algorithm implementation class

Import java.util.*; Public classSolution { PublicList<integer> findsubstring (StringSString[] words) {list<integer> List =NewArraylist<integer> ();if(Words.length = =0) return list;intWlen = words[0].length ();int Len= S.length ();if(Len< Wlen * Words.length) return list; map<String, integer> MAPW =Newhashmap<String, integer> (); for(Stringword:words) mapw.put (Word, mapw.containskey (word)? MAPW.Get(word) +1:1); for(intStart =0; Start < Wlen; start++) {intpos = start;intTstart =-1; map<String, integer> mapt =Newhashmap<String, integer> (MAPW); while(pos + Wlen <=Len) {StringCand = s.substring (pos, pos + wlen);if(!mapw.containskey (cand)) {if(Tstart! =-1) MapT =Newhashmap<String, integer> (MAPW); Tstart =-1; }Else if(Mapt.containskey (cand)) {Tstart = Tstart = =-1?

Pos:tstart;if(MAPT.Get(cand) = =1) Mapt.remove (cand);ElseMapt.put (Cand, MapT.Get(cand)-1);if(MAPT.IsEmpty()) List.add (Tstart); }Else{ while(Tstart < POS) {StringRcand = s.substring (Tstart, Tstart + Wlen);if(Cand.equals (Rcand)) {Tstart + = Wlen;if(MAPT.IsEmpty()) List.add (Tstart); Break } Tstart + = Wlen; Mapt.put (Rcand, Mapt.containskey (rcand)? mapt.Get(Rcand) +1:1); }} pos + = Wlen; }} return list; }}

Assessment Results

  Click on the picture, the mouse does not release, drag a position, release after the new form to view the full picture.

Special Instructions Welcome reprint. Reprint Please specify the source "http://blog.csdn.net/derrantcm/article/details/47064933"

"Leetcode-Interview algorithm classic-java Implementation" "030-substring with concatenation of all Words (concatenation of all words in a substring)"

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.