题目要求: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的更多相关文章

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

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

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

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

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

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

  6. leetcode python 030 Substring with Concatenation of All Words

    ## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...

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

  9. 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. Java学习的第五十六天

    1.例11.5引用保护成员 public class Cjava { public static void main(String[]args) { Student1 s1=new Student1( ...

  2. Linux 系统编程 学习:008-基于socket的网络编程3:基于 TCP 的通信

    背景 上一讲我们介绍了 基于UDP 的通信 这一讲我们来看 TCP 通信. 知识 TCP(Transmission Control Protoco 传输控制协议). TCP是一种面向广域网的通信协议, ...

  3. django路径问题

    1. 初始化项目结构 2.创建Django项目 使用pycharm打开项目 1.右击---->编辑配置 > 2.文件---->设置 > 3.文件---->设置 > ...

  4. PS零基础入门教程--裁剪工具用法

    我是IT轩,分享一下我使用PS的一些用法,希望对大家有帮助!欢迎关注微信公众号:笑林新记 PS版本:PS CC 2019 主要技术:裁剪工具. 裁剪工具主要有:裁剪工具.透视裁剪工具.切片工具和切片选 ...

  5. 浅谈js for循环输出i为同一值的问题

    问题再现 ​ 最近开发中遇到一个问题,为什么每次输出都是5,而不是点击每个p,就alert出对应的1,2,3,4,5. <html> <head> <meta http- ...

  6. 没有磁盘空间 No space left on device

    INSTALL 的解释文件 帮助文件 这里的 pytorch=1.0.1 torchvision=0.2.2 cudatoolkit=9.0,这个ATSS可以运行. 这里最好能够查看一下cuda的版本 ...

  7. JS--遍历对象属性的五种方式

    ES6 一共有 5 种方法可以遍历对象的属性. (1)for...in for...in循环遍历对象自身的和继承的可枚举属性(不含 Symbol 属性). (2)Object.keys(obj) Ob ...

  8. 内网渗透 day4-meterpreter基本命令

    meterpreter基本命令 目录 1.getuid 查看当前用户 1 2.getpid 查看当前的进程id 1 3.getsystem 初步提权 1 4.ps 1.查看进程列表2.帮助我们获取pi ...

  9. pandas_01

    # Pandas 知识点总结 # Pandas数据结构:Series 和 DataFrame import pandas as pd import numpy as np # 一,Series: # ...

  10. quic 2 ietf-transport-draft-ietf-quic-transport-09

    quic的优点 Low-latency connection establishment Multiplexing without head-of-line blocking Authenticate ...