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. 用.native修饰器来对外部组件进行构造器内部方法的调用以及用原生js获取构造器里的方法

    html <div id="app"> <span v-text="number"></span> <btn @cli ...

  2. (转载)MySQL基础(非常全)

    MySQL基础 一.MySQL概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access ...

  3. @PathVariable与@RequestBody的区别,及前段请求接口的写法。

    @PathVariable 1:接受参数时,地址栏为:/{args1}/{args2} 2:用法:(@PathVariable(value = "args")Long  id) 3 ...

  4. Latex: 添加IEEE论文keywords

    参考: How to use \IEEEkeywords Latex: 添加IEEE论文keywords 方法: \begin{IEEEkeywords} keyword1, keyword2. \e ...

  5. 【NOIP 2016】Day1 T2 天天爱跑步

    Problem Description 小 C 同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任 ...

  6. 为DataGridView增加鼠标滚轮功能

    #region 鼠标滚动 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "Wi ...

  7. JavaScript重点知识(二)

    三.JS的API 3.1知识点(DOM) 1)DOM本质 将html结构化成浏览器和JS可识别可操作的东西 2)变量计算---强制类型转换 获取DOM节点 Attribute(对html标签属性的修改 ...

  8. _killerstreak

    `count`连杀或终结连杀的数量(最大支持10个) `announceFlag` 0-不广播1-只广播连杀消息2-只广播终结连杀消息3-广播连杀与终结连杀消息 `rewId` 连杀奖励模板Id,对应 ...

  9. header 格式

    headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,* ...

  10. Tp5.1使用导出Excel

    composer require phpoffice/phpexcel 不管它的警告,都能用的. use PHPExcel; use PHPExcel_IOFactory; public static ...