码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Substring with Concatenation of All Words

时间:2016-01-22 11:04:31      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

LeetCode解题之Substring with Concatenation of All Words


原题

现有一组长度相等的字符串words,要在原字符串中找出正好包含words中所有字符串的子字符串的起始位置。

注意点:

  • 返回结果的顺序没有关系

例子:

输入: s = “barfoothefoobarman”, words = [“foo”, “bar”]

输出: [0, 9]

解题思路

因为words中的单词可能有重复,所以要有一个dict来记录一下每个字符串的数目。然后在遍历原字符串的时候,只需要遍历单词的长度次即可,如”barfoothefoobarman”,因为目标单词的长度为3,所以只需遍历:

  • ‘bar’ | ‘foo’ | ‘the’ | ‘foo’ | ‘bar’ | ‘man’
  • ‘arf’ | ‘oot’ | ‘hef’ | ‘oob’ | ‘arm’
  • ‘rfo’ | ‘oth’ | ‘efo’ | ‘oba’ | ‘rma’

在遍历时,需要两个指针,一个用来标记子字符串的开始,另一个用来标记子字符串的结束。再用一个dict来记录当前字符串中单词的数量,如果下一个单词不在words中,那么清空该dict,把前指针直接跳到后指针处;如果在words中,那么相应的键值要加一,此时如果那个单词的数量超过了目标中的数目,那么前指针要不断后移直到吐出一个那个单词。通过前后指针之差是否等于所有目标单词长度之和来判断是否有目标子字符串。

AC源码

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 = {}
        for word in words:
            words_dict[word] = words_dict[word] + 1 if word in words_dict else 1
        for i in range(word_length):
            left = i
            right = i
            curr_dict = {}
            while right + word_length <= s_length:
                word = s[right:right + word_length]
                right += word_length
                if word in words_dict:
                    curr_dict[word] = curr_dict[word] + 1 if word in curr_dict else 1
                    while curr_dict[word] > words_dict[word]:
                        curr_dict[s[left:left + word_length]] -= 1
                        left += word_length
                    if right - left == words_length:
                        result.append(left)
                else:
                    curr_dict.clear()
                    left = right
        return result


if __name__ == "__main__":
    assert Solution().findSubstring("barfoothefoobarman", ["foo", "bar"]) == [0, 9]

欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。

LeetCode Substring with Concatenation of All Words

标签:

原文地址:http://blog.csdn.net/u013291394/article/details/50559648

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!