[LeetCode]30.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 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).

【分析】

先用map<string,int>统计L中每个单词的个数(L中单词可能会重复)。

从第一个字符开始遍历,对于第i个字符,由它开始和L中单词匹配,如果不匹配,跳到第i+1字符继续匹配。

【代码】

/*********************************
*   日期:2015-01-25
*   作者:SJF0115
*   题目: 30.Substring with Concatenation of All Words
*   网址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <vector>
#include <map>
using namespace std;

class Solution {
public:
    vector<int> findSubstring(string S, vector<string> &L) {
        int strLen = S.length();
        int size = L.size();
        int wordLen = (L[0]).length();
        vector<int> result;
        if(strLen <= 0 || size <= 0){
            return result;
        }//if
        map<string,int> words;
        // word count
        for(int i = 0;i < size;++i){
            ++words[L[i]];
        }//for
        map<string,int> curMap = words;
        // 遍历寻找
        for(int i = 0;i <= strLen - size*wordLen;++i){
            curMap = words;
            // 从位置i寻找words
            for(int j = 0;j < size;++j){
                string substr = S.substr(i+j*wordLen,wordLen);
                // 不匹配
                if(words.find(substr) == words.end()){
                    break;
                }//if
                --curMap[substr];
                if(curMap[substr] < 0){
                    break;
                }//if
                // 一个匹配项
                if(j == size-1){
                    result.push_back(i);
                }//if
            }//for
        }//for
        return result;
    }
};

int main(){
    Solution solution;
    string s("barfoothefoobarman");
    vector<string> vec;
    vec.push_back("foo");
    vec.push_back("bar");
    vector<int> result = solution.findSubstring(s,vec);
    // 输出
    for(int i = 0;i < result.size();++i){
       cout<<result[i]<<endl;
    }//for
    return 0;
}



目录
相关文章
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
|
iOS开发
LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博.
4194 0
|
算法
LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words  Problem's Link  ---------------------------------------------------------------------------...
1026 0
[LeetCode] Substring with Concatenation of All Words
I think the following code is self-explanatory enough. We use an unordered_map counts to record the expected times of each word and another unordered_map seento record the times we have seen.
866 0
|
3月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行