Leetcode Substring with concatenation of all Words

Source: Internet
Author: User

Leetcode problem solving substring with concatenation of the all words original question

An existing set of string words of equal length, to find the starting position of the substring in the original string that exactly contains all the strings in the words.

Note the point:

    • The order in which the results are returned has no relation

Example:

Input: s = "Barfoothefoobarman", words = ["foo", "Bar"]

Output: [0, 9]

Thinking of solving problems

Because the words in words may be duplicated, there is a dict to record the number of each string. Then, when traversing the original string, only need to traverse the length of the word, such as "Barfoothefoobarman", because the target word length is 3, so simply traverse:

    • ' Bar ' | ' Foo ' | ' The ' | ' Foo ' | ' Bar ' | ' Man '
    • ' ARF ' | ' Oot ' | ' Hef ' | ' OOB ' | ' Arm '
    • ' Rfo ' | ' Oth ' | ' Efo ' | ' Oba ' | ' RMA '

When traversing, you need two pointers, one to mark the beginning of the substring, and another to mark the end of the substring. Then use a dict to record the number of words in the current string, if the next word is not in words, then clear the dict, the front pointer jumps directly to the back pointer, if in the words, then the corresponding key value to add one, at this time if the number of words exceeds the number of targets, Then the pointer should be moved back and forward until one of the words is spit out. The target substring is judged by whether the difference between the pointers is equal to the sum of all the target word lengths.

AC Source
 class solution(object):     def findsubstring(self, s, words):        "" : Type S:str:type words:list[str]: rtype:list[int] "" "S_length = Len (s) word_num = Len (words) word_length = Len (words[0]) Words_length = word_num * word_length result = [] Words_dict = {} forWordinchWords:words_dict[word] = Words_dict[word] +1 ifWordinchWords_dictElse 1         forIinchRange (word_length): left = i right = I curr_dict = {} whileRight + word_length <= S_length:word = s[right:right + Word_length] Right + = Word_lengthifWordinchWords_dict:curr_dict[word] = Curr_dict[word] +1 ifWordinchCurr_dictElse 1                     whileCurr_dict[word] > Words_dict[word]: Curr_dict[s[left:left + word_length]]-=1Left + = Word_lengthifRight-left = = Words_length:result.append (left)Else: Curr_dict.clear () left = rightreturnResultif__name__ = ="__main__":assertSolution (). Findsubstring ("Barfoothefoobarman", ["foo","Bar"]) == [0,9]

Welcome to my GitHub (Https://github.com/gavinfish/LeetCode-Python) to get the relevant source code.

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