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 ...
随机推荐
- 题解——P1133 教主的花园DP
直接设二维状态具有后效性,会爆零 然后需要加一维 然后70 看了题解之后发现没有考虑1和n的关系 考虑之后,四十 然后懵逼 突然发现自己的ans更新写错了,导致每次ans都是第一个取30的情况的解 然 ...
- 常用处理数组、字符串API → forEach every some sort map filter slice split indexOf concat substring substr splice join toString replace
Object与Array的语法糖 var arr = [1,2,3]; // [] 是 new Array(1,2,3) 的语法糖(简写) var obj = {'name':2,'age':3}; ...
- Linux下使用wget下载FTP服务器文件
wget -nH -m --ftp-user=your_username --ftp-password=your_password ftp://your_ftp_host/* 使用命令下载ftp上的文 ...
- 基于 Python 和 Pandas 的数据分析(5) --- Concatenating and Appending
这一节我们将会介绍几种不同的合并数据的方法. 在我们这个不动产投资的例子中, 我们希望获取 51 个州的房产数据, 并把它们组合起来. 我们这样做有很多原因. 这样做既便于我们做分析, 同时也可以占用 ...
- P4574 [CQOI2013]二进制A+B
传送门 思路: 本题可用数位DP来做,设 f [ i ][ a ][ b ][ c ][ j ] 表示当前枚举到(二进制下的)第i位,a' b' c'各用a,b,c了几个1,j表示最后一位是否有进位. ...
- python 定时器
2s启动一个定时器: import threading import time def hello(name): print "hello %s\n" % name global ...
- 重装win7系统并激活
备份 大白菜制作启动盘 下载大白菜软件UEFI版(新电脑使用uefi版本,装机版支持的主板多) 选择默认安装 选择默认模式开始制作 下载iso镜像文件,复制到u盘(手动复制) 设置bios ...
- Java中的单实例
前几天刚学完单实例设计模式,今天看代码时发现一行代码很奇怪,getRuntime()函数的返回类型怎么是它本身,忽然想起前几天学的单实例模式,于是找到方法的定义,果然是静态私有变量,获取实例的公有方法 ...
- 学习笔记3—matlab中load特殊用法
1.在matlab中 ,infro.mat中存有很多子矩阵(比如:mean_FA.mat, mean_e1.mat和 mean_e2.mat),调出某一个矩阵时,命令行为:load([path,'\' ...
- U8工具栏特别小是怎么回事
用友的工具栏特别窄了,填制凭证里的保存.增加凭证等按钮因为工具栏特别窄都看不清了 解决方法:正常机器下的system32下面的mscomctl.ocx文件替换到有问题的机器下,您的系统应该是XP的,这 ...