题目:

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.

For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]

You should return the indices: [0,9]
(order does not matter).  (Hard)

分析:

题目意思是找到所有起始位置,使得往后的若干连续字符组成的字符串 的正好是集合中所有元素组成的字符串。

自己考虑到的还是一个暴力的做法,没有超时,写起来也没那么容易。

大致思路是建一个hash存放words不同元素出现在words中的次数(开始只存放有无,后来发现words中元素可重复)

然后对s进行遍历,从每个位置开始,依次取len长度的字符串,与hash中的值比较。根据在不在hash内进行跳出循环,hash[words[j]]--等操作.

到words.size() -1 长度自然跳出循环,说明完全匹配,可以将这个i加入结果。

代码:

 class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
int len = words[].size();
unordered_map<string, int> count;
vector<int> result;
if (s.size() < words.size() * len) {
return result;
}
for (int k = ; k < words.size(); ++k) {
if (count.find(words[k]) == count.end()) {
count[words[k]] = ;
}
else {
count[words[k]] ++;
}
}
unordered_map<string, int> save = count;
for (int i = ; i <= s.size() - words.size() * len; i++) {
int flag = ;
for (int j = i; j < i + words.size() * len ; j += len) {
string temp = s.substr(j, len);
if (count.find(temp) != count.end()) {
if (count[temp] > ) {
count[temp]--;
}
else {
flag = ;
break;
}
}
else {
flag = ;
break;
}
}
if (flag == ) {
result.push_back(i);
}
count = save;
}
return result;
}
};

8月19日更新:

对于自己的实现中,每次更新i都需要拷贝一次hash表。

可以换一种方式,一个hash作为基准在循环前准备好。然后对于每个i生成一个新的hash,添加元素进去,与老的比较,比拷贝应该效率要高。

其他注意事项:

1. words.size(),s.size()都是unsigned int,所以做减法之后不会出现 -1,所以导致由此限定循环有问题,所以自己在前面加了一个

if (s.size() < words.size() * len) 
 搞清楚原因后前面声明两个int变量可以把这个if去掉。
2. unordered_map<string,int> hash可以直接对hash[temp]++,没有的话就是从0加1,默认初始化的0。
所以修改后的代码:
 class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
int len = words[].size();
unordered_map<string, int> count;
vector<int> result;
for (int k = ; k < words.size(); ++k) {
count[words[k]]++;
}
//s.size() - words.size() * len 结果是个unsigned;
int n = s.size(), m = words.size();
for (int i = ; i <= n - m * len; i++) {
unordered_map<string, int> hash;
int flag = ;
for (int j = i; j < i + words.size() * len ; j += len) {
string temp = s.substr(j, len);
hash[temp]++;
if (hash[temp] > count[temp]) {
flag = ;
break;
}
}
if (flag == ) {
result.push_back(i);
}
}
return result;
}
};

好像还有个滑动窗口的解法,回头头脑清楚再研究...

LeetCode30 Substring with Concatenation of All Words的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. SCP和SFTP(转)

    原文:http://www.cnblogs.com/wang_yb/p/3819441.html 不管SCP还是SFTP,都是SSH的功能之一.都是使用SSH协议来传输文件的. 不用说文件内容,就是登 ...

  2. 二、 C#调用存储过程

    个人比较喜欢使用第二种传递参数的方法 1. 调用的方法 public DataTable ExceStoredProcedure (string strCom, SqlParameter[] comm ...

  3. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

  4. Add mappings to an Elasticsearch index in realtime

    Changing mapping on existing index is not an easy task. You may find the reason and possible solutio ...

  5. C#中利用委托实现多线程跨线程操作

    在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了保证线程安全以及提高代码的效率所做的改进,但是也给大家带来很多不便. 其实解决这个 ...

  6. Rule of write code with C# in Unity3d

    Good Practice 普通的Public变量可以在inspect里显示,变量是可以在inspect里赋值并realtime反映在被attach到的GameObject上的.注意经典public ...

  7. 从UnitedStack OS 1.0 Preview试用申请问卷调查学习OpenStack

    http://www.diaochapai.com/survey/ 您的角色最可能是? * (必填, 多选) OpenStack私有云用户,希望能将OpenStack/UOS用于公司内部私有云 云计算 ...

  8. web前端—工作周报

    2016.07.25-2016.07.29周报: 1.本周工作主要内容: A:完成了宏视云h5播放器升级及大数据上报: B:修复xk-h5播放器bug:在三星手机自带浏览器无法进行滑动seek;  C ...

  9. git强制覆盖本地文件

    git fetch --all git reset --hard origin/master

  10. WaitForMultipleObject与MsgWaitForMultipleObjects用法

    http://blog.csdn.net/byxdaz/article/details/5638680 用户模式的线程同步机制效率高,如果需要考虑线程同步问题,应该首先考虑用户模式的线程同步方法. 但 ...