Substring with Concatenation of All Words

原创 2018年04月17日 20:17:40

题目描述

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

For example, given:
S:"barfoothefoobarman"
L:["foo", "bar"]

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

题意:

给一个字符串 S 和一个单词列表,单词长度都一样,找出所有 S 的子串,子串由所有单词组成,返回子串的起始位置。

分析:

很明显每个子串都是由所有单词组成的,长度是一定的,所以直接枚举子串,切分后再用 map 进行判断就行了。

实现代码:

             class Solution {
public:
    vector<int> findSubstring(string S, vector<string> &L) {
        map<string, int> words;  
        map<string, int> curWords;  
        vector<int> ret;  
        int slen = S.length();  
        if (!slen || L.empty()) return ret;  
        int llen = L.size(), wlen = L[0].length();  
        for(int i=0;i<L.size();i++)  
//计算字符串对应的key值
            ++words[L[i]];   
        // check the [llen * wlen] substring  
        for (int i = 0; i + llen * wlen <= slen; i++) 
        {  
            curWords.clear();  
            int j = 0;  
            // check the words  
            for (j = 0; j < llen; j++) 
            {  
                string tmp = S.substr(i + j * wlen, wlen);  
                if (words.find(tmp) == words.end())  
                    break;  
                ++curWords[tmp];  
                if (curWords[tmp] > words[tmp])  //防止S中重复单词出现
                    break;  
            }  
            if (j == llen)  //从i位置已经找到了L中的单词;
                ret.push_back(i);  
        }  
        return ret;  
    }
};

LeetCode(30) 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 s...
  • fly_yr
  • fly_yr
  • 2015-08-24 20:51:05
  • 2447

Substring with Concatenation of All Words -- LeetCode

原题链接: http://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/  这道题看似比较复杂,其实思路和Lon...
  • linhuanmars
  • linhuanmars
  • 2014-03-03 04:00:17
  • 25932

【LeetCode】Substring with Concatenation of All Words 解题报告

【题目】 You are given a string, S, and a list of words, L, that are all of the same length. Find all ...
  • ljiabin
  • ljiabin
  • 2014-12-29 15:45:19
  • 7719

[Leetcode]30. Substring with Concatenation of All Words @python

题目You are given a string, s, and a list of words, words, that are all of the same length. Find all s...
  • qian2729
  • qian2729
  • 2016-01-16 22:19:45
  • 483

LeetCode | Substring with Concatenation of All Words(链接所有单词的子串)

You are given a string, S, and a list of words, L, that are all of the same length. Find all startin...
  • a45872055555
  • a45872055555
  • 2014-08-17 20:54:32
  • 495

LeetCode 30 Substring with Concatenation of All Words (C,C++,Java,Python)

Problem: You are given a string, s, and a list of words, words, that are all of the same length...
  • runningtortoises
  • runningtortoises
  • 2015-05-14 13:13:52
  • 1148

【LeetCode】Substring with Concatenation of All Words

Substring with Concatenation of All Words  Total Accepted: 6263 Total Submissions: 35962 My Submiss...
  • u013027996
  • u013027996
  • 2014-04-30 15:29:28
  • 4358

[LeetCode] 030. Substring with Concatenation of All Words (Hard) (C++/Java)

[LeetCode] 030. Substring with Concatenation of All Words (Hard) (C++/Java)
  • hcbbt
  • hcbbt
  • 2015-03-06 22:14:12
  • 3045

[leetcode] 30. Substring with Concatenation of All Words 解题报告

题目链接:https://leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a stri...
  • qq508618087
  • qq508618087
  • 2015-12-18 04:40:19
  • 640

(Java)LeetCode-30. Substring with Concatenation of All Words

这道题Hard模式,比较复杂,又没有用到很经典的算法,卡在这道题很长时间了。 首先比较直观的解法package datastru; import java.util.ArrayList; imp...
  • u012848330
  • u012848330
  • 2016-07-31 22:03:21
  • 319
收藏助手
不良信息举报
您举报文章:Substring with Concatenation of All Words
举报原因:
原因补充:

(最多只允许输入30个字)