Substring with concatenation of all Words

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

Analysis:

The first way: add words to HashMap, and then starting from I, one by one to see if each word is in the hashmap, if present, then HashMap word in the corresponding count-1, if HashMap is empty, This means that all words have been acquired and the starting index is added to the list.

But it's not going to be the way. Wipe.

1  Public classSolution {2      PublicList<integer>findsubstring (String s, string[] words) {3list<integer> list =NewArraylist<>();4         if(s = =NULL|| S.length () = =0|| Words = =NULL|| Words.length = =0)5             returnlist;6 7         intLength = words[0].length ();8          for(inti =0; I < s.length (); i++) {9map<string, integer> map =Getmap (words);Ten             intindex =i; OneBoolean allcontained =true; A              while(!Map.isempty ()) { -                 if(Index + length >s.length ()) { -allcontained =false; the                      Break; -                 } -String sub = s.substring (index, Index +length); -                 if(Map.containskey (sub)) { +Map.put (sub, map.Get(sub)-1); -                     if(Map.Get(sub) = =0) { + Map.Remove (sub); A                     } atIndex + =length; -}Else { -allcontained =false; -                      Break; -                 } -             } in  -             if(allcontained) { to List.add (i); +             } -         } the         returnlist; *     } $ Panax Notoginseng     PrivateMap<string, integer>Getmap (string[] words) { -map<string, integer> map =NewHashmap<>(); the  +          for(String str:words) { AMap.put (str, map.getordefault (str,0) +1); the         } +         returnmap; -     } $}

Another way is to first a window to cover s, so that the word in the words is covered in the substring, then for the next, we need to move the left part of the window to the right word out, so that the right can continue to move. We also need to move the pointer on the left side of the window if the word we are adding to the right is already enough, or not in words. It is a pity that this method will not go through, no matter.

1  PublicList<integer>findsubstring (String s, string[] words) {2Map<string, integer> all =Getmap (words);3list<integer> list =NewArraylist<>();4         if(s = =NULL|| S.length () = =0|| Words = =NULL|| Words.length = =0)5             returnlist;6 7         intLength = words[0].length ();8          for(inti =0; i < length; i++) {9map<string, integer> map =Getmap (words);Ten             intindex =i; One             intstarting =i; A              while(Index + length <=s.length ()) { -String sub = s.substring (index, Index +length); -                 if(Map.containskey (sub)) { theMap.put (sub, map.Get(sub)-1); -                     if(Map.Get(sub) = =0) { - Map.Remove (sub); -                     } +                     //Find All the words in the HashMap -                     if(Map.isempty ()) { + List.add (starting); AMap.put (S.substring (starting, starting + length),1); atstarting = starting +length; -                     } -}Else { -                     if(All.containskey (sub)) { -Map.put (S.substring (starting, starting +length), -Map.getordefault (S.substring (starting, starting + length),0) +1); instarting = starting +length; -                         Continue; to}Else { +Map =Getmap (words); -Starting = index +length; the                     } *                 } $Index + =length;Panax Notoginseng             } -         } the         returnlist; +     } A  the     PrivateMap<string, integer>Getmap (string[] words) { +map<string, integer> map =NewHashmap<>(); -  $          for(String str:words) { $Map.put (str, map.getordefault (str,0) +1); -         } -         returnmap; the}

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.