Stay button series of all words substring solution (Python)

Stay button series of all words substring Solution

Description Title:
Given a string s and a word length of some of the same words. Find the starting position of the substring s may be formed in exactly words all words in series form.

Note that exactly match the substring to words in a word, there are no intervening other characters, but does not need to consider the order of words in a series of words.

Example 1:
Input:
S = "barfoothefoobarman",
words = [ "foo", "bar"]
Output: [0,9]
Explanation:
string from the sub-indexes 0 and 9 respectively start "barfoo" and "foobar".
Order of the output is not important, [9,0] is a valid answer.
Example 2:
Input:
S = "wordgoodgoodgoodbestword",
words = [ "Word", "Good", "Best", "Word"]
Output: []

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/substring-with-concatenation-of-all-words
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Reference Program 1:

class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        if not words: return []
        if not s: return []
        length_words = len(words[0])
        result = []
        for index in range(len(s)-length_words*len(words)+1):
            i = index
            words_temp = []
            while(i<index+length_words*len(words)):
                if s[i:i+length_words] in words:
                    words_temp.append(s[i:i+length_words])
                    i += length_words
                else:
                    break
            if len(words)==len(words_temp) and sorted(words)==sorted(words_temp):
                result.append(index)        
        return result

Results Run 1:
Here Insert Picture Description
Reference Program 2:

class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        if not words:
            return []
        
        words_dict = collections.defaultdict(int)
        for word in words:
            words_dict[word] += 1
            
        s_length, words_length, word_length, result = len(s), len(words), len(words[0]), list()
        for k in range(word_length):
            head_words, num = collections.defaultdict(int), 0
            for i in range(k, s_length, word_length):
                word = s[i:i + word_length]
                if word in words_dict:
                    num += 1
                    head_words[word] += 1
                    while head_words[word] > words_dict[word]:
                        pos = i - word_length*(num - 1)
                        temp_word = s[pos:pos + word_length]
                        head_words[temp_word] -= 1
                        num -= 1
                else:
                    head_words.clear()
                    num = 0
                    
                if num == words_length:
                    result.append(i - word_length*(num - 1))
        return result

Results Run 2:
Here Insert Picture Description
Reference Program 3:

class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        words_s = sorted(words)
        result = []
        if len(words_s)==0:
            return result
        len_eachword = len(words_s[0])
        len_word = len(words_s)
        total_len = len_eachword*len_word
        if len(s)<total_len:
            return result
        for word in set(words_s):
            for i in range(len(s)-total_len+1):
                checklist_s = list()
                if s[i:i+len_eachword] == word:
                    for j in range(i,i+total_len,len_eachword):
                        checklist_s.append(s[j:j+len_eachword])
                    checklist_s = sorted(checklist_s)
                    if checklist_s == words_s:
                        result.append(i)
        return result

Run Results:
Here Insert Picture Description

Published 16 original articles · won praise 0 · Views 171

Guess you like

Origin blog.csdn.net/chutu2018/article/details/104800161