题解 | #牛牛的串联子串游戏#
牛牛的串联子串游戏
https://www.nowcoder.com/practice/c1984371372b43f3b10bf6d0231520bb
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @param words string字符串一维数组 # @return int整型一维数组 # class Solution: def findSubstring(self, s, words): if not s or not words: return [] word_len = len(words[0]) total_words_len = word_len * len(words) result = [] word_count = {} for word in words: word_count[word] = word_count.get(word, 0) + 1 for i in range(word_len): left = i right = i curr_count = {} while right + word_len <= len(s): curr_word = s[right:right + word_len] right += word_len if curr_word in word_count: curr_count[curr_word] = curr_count.get(curr_word, 0) + 1 while curr_count[curr_word] > word_count[curr_word]: curr_count[s[left:left + word_len]] -= 1 left += word_len if right - left == total_words_len: result.append(left) else: curr_count.clear() left = right return result