Substring with Concatenation of All Words

来源:互联网 发布:centos 6.5配置hadoop 编辑:程序博客网 时间:2024/05/16 09:23

Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters.

For example, given:
s"barfoothefoobarman"
words["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

滑动窗口的神题。思路和 Minimum Window Substring 非常相似。
但是这个题tricky一点在于要发现,只要遍历 string  每个单词长度(K) 遍 就可以包含全部情况。

几个容易出错的地方在代码中有标注。

这题是要求 without intervening characters的。这里跟  Minimum Window Substring 要求不同。所以就导致了再一次遍历中存在着重新初始化的情况(map初始化)
这一点应该是可以提升的,毕竟每次遇见intervening string就重启一个大的map很耗时。如果可以只修复map被污染的部分会好些。
0 0
原创粉丝点击