[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;
}

时间: 2024-10-07 14:19:32

[LeetCode]30.Substring with Concatenation of All Words的相关文章

LeetCode 30 Substring with Concatenation of All Words(与所有文字串联子串)(*)

翻译 给定一个字符串S,一个单词的列表words,全是相同的长度. 找到的子串(多个)以s即每个词的字串联恰好一次并没有任何插入的字符所有的起始索引. 原文 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

[LeetCode] Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

&lt;font color=&quot;red&quot;&gt;[置顶]&lt;/font&gt;

Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸,本博客会持续更新,感谢您的支持,欢迎您的关注与留言.博客有多个专栏,分别是关于 Windows App开发 . UWP(通用Windows平台)开发 . SICP习题解 和 Scheme语言学习 . 算法解析 与 LeetCode等题解 . Android应用开发 ,而最近会添加的文章将主要是算法和Android,不过其它内容也会继续完善. About the Author 独立 Windows App 和

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

请问,ASP.net如何把带html格式的内容转换成纯文本的文字

问题描述 请问,ASP.net如何把带html格式的内容转换成纯文本的文字 解决方案 解决方案二:使用正则表达式对字符串进行过滤解决方案三:publicstringLostHTML(stringStr){stringRe_Str="";if(Str!=null){if(Str!=string.Empty){stringPattern="<\/*[^<>]*>";Re_Str=Regex.Replace(Str,Pattern,"&q

[LeetCode]76.Minimum Window Substring

题目 Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such wi

[LeetCode]5.Longest Palindromic Substring

题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路一 即不使用技巧,穷举所有可能.时间复杂度为O(n^3)(时间上最长,不推荐使用),空间复杂度为O(1).本思路是从最大长度的字串开始,而不