[Leetcode] 30. Substring with concatenation of all Words ideas for solving-Java

Source: Internet
Author: User

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, a concatenation of each word in wordsexactly once and without any I ntervening characters.

For example, given:
S"barfoothefoobarman"
Words["foo", "bar"]

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

Problem: Given a string s and a list of words words. Find the starting subscript for all substrings in s that meet all of the following conditions: requires that the substring is exactly a concatenation of all the words of words, and that each word appears only once.

Because the words inside the words are unordered, the member words of the target substring are also unordered, so consider using Hashtable as the data store.

The two subject conditions that need to be fully utilized:

1. All words equal length

2. The target substring is a continuous, uninterrupted connection of the word

According to the above two conditions, we can divide the s all into substrings of length, and there is a total of length method. The length method overrides all of the substrings that need to be found.

For each segment, a substring of length can be treated as a cell that is no longer split, using the sliding window algorithm (Slide window algorithm) to find a target substring that meets the criteria in linear time , O (n/length) complexity, where n is the length of S. For a total of length distributions, it takes time O (length * n/length) = O (n).

algorithm Implementation Ideas :

The method of dividing s all into successive substrings of length lengths, with a total of length.

For each k (0 <= K < length, the starting subscript for starting the split) is:

The starting position is k, and the length substring is a substring to be validated Subs

Left and right hands pointing to the first subs

If the right pointer to subs is the word you are looking for, and the number of times you have logged is less than the number of times you need to find it, the record finds a subs, and the right pointer moves right.

If the right pointer to subs is the word you are looking for, and the number of times that you have recorded is equal to the number of times you need to find it, exclude the current word from the record and the left pointer to the right, and repeat until you exclude a right pointer to the current subs from the record. So that [subs has been recorded less than the number of times it needs to be found].

If the right pointer is pointing to a subs that is not the word you are looking for, the right pointer moves right, the left pointer points to the right pointer position, and the condition is recorded.

If the found record (left and right pointer elements) exactly equals the need to find all words, save the position of the left pointer, which is a subscript to look for. The left pointer is then excluded from the record, and the left pointer shifts to the right.

Implementation Code :

Importjava.util.Hashtable;Importjava.util.LinkedList;Importjava.util.List;ImportJava.util.Map.Entry;ImportJava.util.Set;classutility{/*** Reset the value of matched result str_cnt_mch * *@paramSTR_CNT_MCH*/     Public Static voidResethashtable (hashtable<string, integer>str_cnt_mch) {Set<entry<string, integer>> set =Str_cnt_mch.entryset ();  for(Entry<string, integer>S_c:set) {S_c.setvalue (0); }    }} Public classSolution {
PublicList<integer>findsubstring (String s, string[] words) {List<Integer> res =NewLinkedlist<integer>(); Hashtable<string, integer> str_cnt =NewHashtable<string, integer>(); Hashtable<string, integer> str_cnt_mch =NewHashtable<string, integer>(); for(String wrd:words) {if(Str_cnt.containskey (WRD)) {str_cnt.put (WRD, Str_cnt.get (WRD)+ 1); } Else{str_cnt.put (WRD,1); } str_cnt_mch.put (WRD,0); } intLength = Words[0].length (); for(intk = 0; K < length; k++) { intLFT =K; intRgh =K; Utility.resethashtable (STR_CNT_MCH); intmchcnt = 0; while(Rgh + length <=s.length ()) {String Subs= S.substring (Rgh, Rgh +length); if(Str_cnt.containskey (subs)) {if(Str_cnt_mch.get (Subs) <Str_cnt.get (Subs)) {Str_cnt_mch.put (Subs, Str_cnt_mch.get (subs)+ 1); Mchcnt++; Rgh+=length; } Else { //The number of subs in Str_cnt_mch are the same as that//In str_cnt while(true) {String SUBSL= S.substring (LFT, LfT +length); Str_cnt_mch.put (SUBSL, Str_cnt_mch.get (SUBSL)-1); Mchcnt--; LfT+=length; if(Subsl.equals (subs)) { Break; } } } } Else{//Subs is not a word in words utility.resethashtable (STR_CNT_MCH); Mchcnt= 0; Rgh= Rgh +length; LfT=Rgh; } if(mchcnt = =words.length) {Res.add (LFT); String SUBSL= S.substring (LFT, LfT +length); Str_cnt_mch.put (SUBSL, Str_cnt_mch.get (SUBSL)-1); Mchcnt--; LfT= LfT +length; } } } returnRes; }}

Resources:

Substring with concatenation of all Words-Leetcode, Code_ganker, CSDN

[Leetcode] 30. Substring with concatenation of all Words ideas for solving-Java

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.