题目要求: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. python实现登录验证系统(搭建MVC框架)

    小型登录注册验证系统 关注公众号"轻松学编程"了解更多. 一.概述 ​ 使用Redis+MySQL数据库实现一个小型的登录注册验证系统.在这个系统中初步了解认识MVC框架. ​ 具 ...

  2. POJ2432 Around the world

    题意描述 Around the world 在一个圆上有 \(n\) 点,其中有 \(m\) 条双向边连接它们,每条双向边连接两点总是沿着圆的最小弧连接. 求从 \(1\) 号点出发并回到 \(1\) ...

  3. C语言100题集合-ex003

    系列文章<C语言经典100例>持续创作中,欢迎大家的关注和支持. 喜欢的同学记得点赞.转发.收藏哦- 后续C语言经典100例将会以pdf和代码的形式发放到公众号 欢迎关注:计算广告生态 即 ...

  4. linux的mysql数据库创建和删除

    mysql -h localhost -u 用戶名 -p密碼                //连接数据库use desk_show;                                 ...

  5. CF1271E Common Number

    数学+二分 连续打了3场$codeforces$,深深的被各种模拟贪心分类讨论的$C$,$D$题给恶心到了 还有永远看到题一脸懵的$B$题 首先考虑画出不同函数值迭代转移的关系,要注意考虑连边是否能成 ...

  6. 关于layui图片/文件上传

    一:常规使用   普通文件上传 (传入服务器一张图片) 1.前台代码: <!DOCTYPE html><html><head> <meta charset=& ...

  7. /etc/resolv.conf文件自动恢复的解决方法

    /etc/resolv.conf文件自动恢复的解决方法: service NetworkManager stop #后台进程关闭 chkconfig NetworkManager off #配置关闭, ...

  8. 找回了当年一篇V4L2 linux 摄像头驱动的博客

    从csdn找回 , 无缘无故被封了..当时损失不少啊!!!!!!!!! linux 摄像头驱动 :核心数据结构:    /**     * struct fimc_dev - abstraction ...

  9. MySQL架构(面)

    和其它数据库相比,MySQL有点与众不同,它的架构可以在多种不同场景中应用并发挥良好作用.主要体现在存储引擎的架构上,插件式的存储引擎架构将查询处理和其它的系统任务以及数据的存储提取相分离.这种架构可 ...

  10. Linux踩坑之云服务器 ssh 连接不上

    前奏:今天没事处理一下之前远程不了Linux桌面的问题时,找到一个解决方法(开始入坑):                     systemctl set-default graphical.tar ...