题目要求: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).

分析:

参考网址:http://www.cnblogs.com/panda_lin/archive/2013/10/30/substring_with_concatenation_of_all_words.html

代码如下:

class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) { int l_size = L.size(); if (l_size <= 0) {
return vector<int>();
} vector<int> result;
map<string, int> word_count;
int word_size = L[0].size();
int i, j; for (i = 0; i < l_size; ++i) {
++word_count[L[i]];
} map<string, int> counting; for (i = 0; i <= (int)S.length() - (l_size * word_size); ++i) { counting.clear(); for (j = 0; j < l_size; ++j) {
string word = S.substr(i + j * word_size, word_size); if (word_count.find(word) != word_count.end()) {
++counting[word]; if (counting[word] > word_count[word]) {
break;
}
}
else {
break;
}
} if (j == l_size) {
result.push_back(i);
}
} return result;
}
};

LeetCode 030 Substring with Concatenation of All Words的更多相关文章

  1. Java for LeetCode 030 Substring with Concatenation of All Words【HARD】

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  2. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  3. leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...

  4. 【leetcode】Substring with Concatenation of All Words

    Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...

  5. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  6. leetcode python 030 Substring with Concatenation of All Words

    ## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...

  7. [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 sta ...

  8. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  9. Java [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 a ...

随机推荐

  1. [NOIP 2016D2T2/Luogu P1600] 天天爱跑步 (LCA+差分)

    待填坑 Code //Luogu P1600 天天爱跑步 //Apr,4th,2018 //树上差分+LCA #include<iostream> #include<cstdio&g ...

  2. 『JVM』我不想知道我是怎么来滴,我就想知道我是怎么没滴

    我是风筝,公众号「古时的风筝」,一个兼具深度与广度的程序员鼓励师,一个本打算写诗却写起了代码的田园码农! 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在 ...

  3. 11/6笔记 补充(Redis持久化,RDB&&AOF)

    11/6补充笔记 修改redis-6379.conf里面的save10秒2个数据发生改变 (save 10 2) 修改一次数据不发生改变,修改2次数据才发生改变 继续修改数据,发现还是一样的规律 增删 ...

  4. Java线程状态及切换

    Java线程状态及切换 一.什么是Java线程状态 在Java程序中,用于描述Java线程的六种状态: 新建(NEW):当前线程,刚刚新建出来,尚未启动. 运行(RUNNABLE):当前线程,处于竞争 ...

  5. 虚拟机中安装Centos 7

    VMware中安装centos7系统 一.首先需要准备必要文件 1.VMware软件的安装包,建议使用12以上版本 VMwareWorkstation14版本下载链接 链接:https://pan.b ...

  6. 论文阅读:An End-to-End Network for Generating Social Relationship Graphs

    论文链接:https://arxiv.org/abs/1903.09784v1 Abstract 社交关系智能代理在人工智能领域中越来越引人关注.为此,我们需要一个可以在不同社会关系上下文中理解社交关 ...

  7. vpp dpdk 安装使用笔记

    编译安装: make install-dep   make build 编译 vpp 查看 pci 网卡 id : lshw -class network -businfo DPDK hugepage ...

  8. spark-submit提交python脚本过程记录

    最近刚学习spark,用spark-submit命令提交一个python脚本,一开始老报错,所以打算好好整理一下用spark-submit命令提交python脚本的过程.先看一下spark-submi ...

  9. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)'))) - skipping

    C:\Users>pip listPackage Version------------ -------behave 1.2.6configparser 3.7.4ddt 1.2.1parse ...

  10. if __name__ == "__main__"的疑惑

    Python中if __name__ == "__main__"详细解释: 想必很多初次接触python都会见到这样一个语句,if __name__ == "__main ...