ITPub博客

首页 > > > [LeetCode] Substring with Concatenation of All Words

[LeetCode] Substring with Concatenation of All Words

作者:Ruiyuan Li 时间:2015-04-25 00:00:00 0 删除 编辑

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

解题思路:

1、最开始考虑用DFS的办法,将words组合成一个长串,然后用KMP查找,超时错误。到网上查找了相关的代码,大体就是滑动窗口的思想。讲解见http://bangbingsyb.blogspot.com/2014/11/leetcode-substring-with-concatenation.html。

class Solution {
public:
    vector findSubstring(string S, vector &L) {
        vector allPos;
        if(L.empty()) return allPos;
        int totalWords = L.size();
        int wordSize = L[0].size();
        int totalLen = wordSize * totalWords;
        if(S.size() wordCount;
        for(int i=0; i &wordCount, int wordSize, int totalWords) {
        if(S.size()-start+1 < wordSize*totalWords) return false;
        unordered_map wordFound;
        
        for(int i=0; iwordCount[curWord]) return false;
        }
        return true;
    }
};


<!-- -->

原文作者:;如有侵权,请联系管理员删除。

上一篇: Windows 远程登录
请登录后发表评论 登录
全部评论

注册时间:2020-05-21

  • 博文量
    495
  • 访问量
    4826