LeetCode 030 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 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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 【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 ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- leetcode python 030 Substring with Concatenation of All Words
## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...
- [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 ...
- 【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 ...
- 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 ...
随机推荐
- 840. Magic Squares In Grid ——weekly contest 86
题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...
- Servlet学习笔记(四)
目录 Servlet学习笔记(四) 一.会话技术Cookie.session 1. 什么是会话技术? 2. 会话技术有什么用? 3. Cookie 3.1 什么是Cookie? 3.2 使用Cooki ...
- [POJ 2821]TN's Kindom III(任意长度循环卷积的Bluestein算法)
[POJ 2821]TN's Kindom III(任意长度循环卷积的Bluestein算法) 题面 给出两个长度为\(n\)的序列\(B,C\),已知\(A\)和\(B\)的循环卷积为\(C\),求 ...
- 深入IOC及其启动原理
IOC总结 1. IOC概述 三个问题: IOC是什么 为什么用它 怎么用 1.1 是什么? 两个概念:控制反转,依赖注入 来看一下传统的干活方式:在对象单一职责原则的基础上,一个对象很少有不依赖其他 ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- leetcode7:binary-tree-preorder-traversal
题目描述 求给定的二叉树的前序遍历. 例如: 给定的二叉树为{1,#,2,3}, 1 \ 2 / 3 返回:[1,2,3]. 备注:用递归来解这道题太没有新意了,可以给出迭代的解法么? ...
- Oracle数据库系统结构(一)
1.Oracle数据库系统结构概述 Oracle数据库由存放在磁盘上的数据库(DB)和对磁盘上的数据库进行管理的数据库管理系统(DBMS)两部分构成,分别对应着数据库的存储结构和软件结构. Oracl ...
- 5 MVVM
1.概述 MVVM各个部分功能如下: Model:定义业务逻辑 View:定义面向用户接口,UI逻辑,处理用户交互请求 ViewModel:负责界面导航逻辑和应用状态管理,呈现逻辑. 1.1. 各司其 ...
- 前端动画框架GSAP框架随笔
gsap是目前非常流行的前端动画框架,可以非常轻松构造出复杂的动画效果,这里仅对我实际使用中的一些例子进行总结 官网 示例 文章种所使用代码的在线示例 基础用法 // 声明一个滚动控制器 let ct ...
- 手把手教你使用Vuex(一)
1.定义 vuex 是一个专门为vue.js应用程序开发的状态管理模式. 这个状态我们可以理解为在data中的属性,需要共享给其他组件使用的部分.也就是说,是我们需要共享的data,使用vuex进行统 ...