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 目录

leetcode-algorithms-30 Substring with Concatenation of All Words的更多相关文章

  1. [Leetcode][Python]30: Substring with Concatenation of All Words

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

  2. 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 ...

  3. 【一天一道LeetCode】#30. Substring with Concatenation of All Words

    注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...

  4. 【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 ...

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

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

  6. 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 ...

  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] 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 ...

  9. 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 ...

  10. 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. (转) GAN论文整理

    本文转自:http://www.jianshu.com/p/2acb804dd811 GAN论文整理 作者 FinlayLiu 已关注 2016.11.09 13:21 字数 1551 阅读 1263 ...

  2. HDU-6033 Add More Zero

    There is a youngster known for amateur propositions concerning several mathematical hard problems. N ...

  3. vscode "没有活动的源代码控制提供程序" 解决办法

    确保安装git F1,输入Show built-in Extensions, 左侧列表中找Git,然后启用即可

  4. Spring-MVC依赖

    <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api& ...

  5. [从零开始搭网站七]CentOS上安装Mysql

    点击下面连接查看从零开始搭网站全系列 从零开始搭网站 通过前面6章,我们买好了服务器,配置了服务器连接,服务器上配置了JDK和Tomcat,准备了域名(这个我没教,自己去阿里/百度/腾讯买,买东西我相 ...

  6. spring boot配置druid数据源和监控配置

    直接上代码: 一.pom.xml中添加依赖 <dependency> <groupId>com.github.drtrang</groupId> <artif ...

  7. python网络编程基础之socket粘包现象

    粘包现象两种 登陆 #服务端import json import socket server=socket.socket()#创建socket对象 ip_port=('127.0.0.1',8001) ...

  8. Xshell5中常用linux服务器命令集合

    简易版:http://www.zhimengzhe.com/linux/84546.html 详细版:http://www.cnblogs.com/peida/tag/%E6%AF%8F%E6%97% ...

  9. MySQL 的 DISTINCT 应用于2列时

    SELECT DISTINCT vend_id告诉MySQL只返回不同(唯一)的 vend_id行,也就是在vend_id 有重复的行中,只保留一行,其他的不作输出.比如我创建了如下的student表 ...

  10. [JS]给String对象添加方法,使传入的字符串字符之间以空格分开输出

    看到一个这样子的面试题: 给String对象添加一个方法,传入一个string类型的参数,然后将string的每一个字符间加空格返回,例如:addSpace("hello world&quo ...