题目要求: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. Redis学习五(Redis 阻塞的原因及其排查方向).

    一.慢查询 因为 Redis 是单线程的,大量的慢查询可能会导致 redis-server 阻塞,可以通过 slowlog get n 获取慢日志,查看详情情况. 二.bigkey 大对象 bigke ...

  2. R语言factor类型转numeric

    R 语言中为了进行数据分析,比如回归分析,这时候对于数据表格中的factor类型的数据会带来弊端,比如对因子的每一个数据都进行一次回归,这样就显得很复杂,且违背了我们的初衷,需要把factor转换为n ...

  3. sdsdsd

    create PROCEDURE b2(in c_year int,in co int)begin declare num int; if exists(select * from class whe ...

  4. Redis快速入门教程

    1.Redis介绍 Redis说白了就是个存放Key-Value数据接口的内存存储系统,主要用作数据库缓存和消息代理. 内部支持sring,hash,list,set,sorted-set五种数据结构 ...

  5. python插入数据库mysql

    #-*- coding:utf-8 -*- import MySQLdb #alter table test add index prefixIdx_test(ext(2));//前缀索引 try: ...

  6. 2. Hive常见操作命令整理

    该笔记主要整理了<Hive编程指南>中一些常见的操作命令,大致如下(持续补充中): 1. 查看/设置/修改变量2. 执行命令3. 搜索相关内容4. 查看库表信息5. 创建表6. 分区7. ...

  7. 实验3ss

    1.实验任务1 #include <math.h> #include <stdio.h> int main() { float a,b,c,x1,x2; float delta ...

  8. C/C++中内存对齐问题的一些理解(转)

    内存对齐指令 一般来说,内存对齐过程对coding者来说是透明的,是由编译器控制完成的 如对内存对齐有明确要求,可用#pragma pack(n)指定,以n和结构体中最长数据成员长度中较小者为有效值 ...

  9. VS2017新建MVC+ORM中的LinqDb访问数据库项目

    1.前提概述 ORM对象关系映射(Object-Relational Mapping)是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换.从效果上说,它其实是创建了一个可在编程语言 ...

  10. 调整PG分多次调整和一次到位的迁移差别分析

    前言 这个问题来源于我们研发的一个问题,在进行pg调整的时候,是一次调整到位好,还是分多次调整比较好,分多次调整的时候会不会出现某个pg反复挪动的问题,造成整体迁移量大于一次调整的 最近自己的项目上也 ...