leetcode-algorithms-30 Substring with Concatenation of All Words
leetcode-algorithms-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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
Example 1:
Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:
Input:
s = "wordgoodstudentgoodword",
words = ["word","student"]
Output: []
解法
words里都是相同长度的字符串,由此我们可得到需要匹配的字符串的长度.然后在主串里取出该长度的字条串,到words里去匹配.那该怎么匹配呢,可以把所有的word存到一个map里,计算其出现的次数.在需要匹配的字符串里把每个word取出来,判断是否在map里,如果存在把它也存在另个map1里(这个的目的是出现word重复里要判断出现的次数)然后和map比较,若次数大于就表示不匹配.
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words)
{
std::vector<int> res;
if (s.size() == 0 || words.size() == 0)
return res;
//count word
std::unordered_map<string, int> stats;
for (std::string word : words)
stats[word]++;
int n = s.size();
int num = words.size();
int len = words[0].length();
for (int i = 0; i < n - num * len + 1; i++)
{
std::unordered_map<string, int> subword;
int j = 0;
for (; j < num; j++)
{
std::string word = s.substr(i + j * len, len);
if (stats.find(word) != stats.end())
{
subword[word]++;
if (subword[word] > stats[word])
break;
}
else
break;
}
if (j == num)
res.push_back(i);
}
return res;
}
};
时间复杂度: O(n * m).n是字条串长度,m是word的个数.
空间复杂度: O(2m).m是word的个数.
leetcode-algorithms-30 Substring with Concatenation of All Words的更多相关文章
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- LeetCode HashTable 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】#30. Substring with Concatenation of All Words
注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...
- 【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 - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- leetcode面试准备: Substring with Concatenation of All Words
leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...
- [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] 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 ...
- 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 ...
随机推荐
- sed命令使用详解
内容来自马哥视频,感谢马哥精彩讲解 sed:编辑器 sed: Stream EDitor, 行编辑器,逐行进行处理 grep:实现文本过滤 awk:文本报告生成器 sed默认不编辑源文件,而是 ...
- ABAP-FI常用BAPI
总帐会计: (比较简单全部测试通过,关帐时使用) Line item of document for ledger with summary table GL F: BAPI_GLX_GETDOCI ...
- HDU 5791 Two(LCS求公共子序列个数)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5791 题意: 给出两个序列,求这两个序列的公共子序列的总个数. 思路: 和LCS差不多,dp[i][ ...
- React内三种函数的写法
以下提供三种React内函数的写法,都可以正常运行,有疑问可以留言 写法一:让函数内部的this指向这个类的实例,它是用bind实现的,bind的第一个参数表示context,就是this. //写 ...
- bean标签常用属性
scope属性: singleton:只有一个 prototpye:每次创建都是新的 对象初始化方法: init-method 对象销毁方法: destroy-method
- Codeforces 781D Axel and Marston in Bitland
题目链接:http://codeforces.com/contest/781/problem/D ${F[i][j][k][0,1]}$表示是否存在从${i-->j}$的路径走了${2^{k}} ...
- /usr/bin/perl:bad interpreter:No such file or directory 的解决办法
yum -y install gcc gcc-c++ perl make kernel-headers kernel-devel 可能会提示:Cannot find a valid baseurl f ...
- Oracel中的NVL函数
Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...
- [osg]OSG使用更新回调来更改模型
使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调来实现在每帧的更新遍历(update traversal)中进行节点的更新. 回调概览 用户可以使用回调来实现与场景 ...
- 《剑指offer》第五十三题(0到n-1中缺失的数字)
// 面试题53(二):0到n-1中缺失的数字 // 题目:一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字 // 都在范围0到n-1之内.在范围0到n-1的n个数字中有且只有一个数 ...