Substring with concatenation of all Words, returns the position of strings that contain strings of string elements concatenated by strings

Source: Internet
Author: User

Problem Description: Given a character array words, and the string s, returns the position of a substring of all the character elements in the character array in the string, requiring all elements in the string array to exist only once in the string s.

Algorithmic analysis: This problem is similar to STRSTR. Just Strstr is a substring, and the question is a substring of elements in a string array, and the elements in the string array are unordered, but must all be included. All consider using the map collection. The key point is a few critical values, the string element repeated in s how to do, find a matching substring after how to do, there is a string element mismatch how to do.

Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List; Public classSubstringwithconcatenationofallwords { PublicList<integer>findsubstring (String s, string[] words) {ArrayList<Integer> result =NewArraylist<integer>(); if(s==NULL|| S.length () ==0| | words==NULL|| Words.length==0){            returnresult; }              //frequency of wordshashmap<string, integer> map =NewHashmap<string, integer>();  for(String w:words) {if(Map.containskey (w)) {map.put (W, Map.get (w) )+1); }Else{map.put (W,1); }        }             intLen = words[0].length ();  for(intj=0; j<len; J + +) {HashMap<string, integer> currentmap =NewHashmap<string, integer>(); intstart = j;//starting subscript, because Len is the size of the window, so the starting subscript simply loops between the 0-len, just like modulo, over and over and over again.             intCount = 0;//record the number of strings that are satisfied                  for(intI=j; I<=s.length ()-len; i=i+Len) {String Sub= S.substring (i, i+Len); if(Map.containskey (sub)) {if(Currentmap.containskey (sub)) {Currentmap.put (Sub, currentmap.get ( sub))+1); }                    Else{currentmap.put (sub,1); } Count++; //whenever a substring is found to have elements in a repeating character array, the string is removed from the starting position. //In other words, the reverse operation is repeated until it contains only one repeating string.                      while(Currentmap.get (sub) >Map.get (sub)) {                        //four basic reverse operation. The window slide is repeated or the match succeeds. String left = s.substring (Start, start+Len); Currentmap.put (left, Currentmap.get)-1); Count--; Start= Start +Len; }                              if(count==words.length)//The string contains all the elements in the character array,{result.add (start);//Add starting position to result//four basic reverse operation. The window slides. Window value is LenString left = s.substring (Start, start+Len); Currentmap.put (left, Currentmap.get)-1); Count--; Start= Start +Len; }                }                Else{currentmap.clear (); Start= i+Len; Count= 0; }            }        }             returnresult; }}

The STRSTR algorithm is as follows:

 Public intstrStr (String haystack, string needle) {if(Haystack = =NULL|| Needle = =NULL)        {            return-1; }                 for(inti = 0; I <= (Haystack.length ()-needle.length ()); i++)         {            intm =i;  for(intj = 0; J < Needle.length (); J + +)             {                if(Needle.charat (j) = =Haystack.charat (M)) {m++; if((m-i) = =needle.length ()) {                        returni; }                }                 Else                 {                     Break; }            }        }        return-1; }

Substring with concatenation of all Words, returns the position of strings that contain strings of string elements concatenated by strings

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.