LeetCode30 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.
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)
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的更多相关文章
- 【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 ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- 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 ...
- [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 ...
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 关于Bean
什么是Bean? 一个Bean 就是一个类.我们不必为制造任何的Bean而编写一些特殊的代码或者使用特殊的编程语言.事实上,我们唯一需要做的是略微地修改我们对我们方法命名的办法.方法名通知应用程序构建 ...
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- [POJ] #1006# Biorhythms : 最小公倍数/同余问题
一. 题目 Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127263 Accepted: 403 ...
- jxse2.6在jdk8下,JxtaMulticastSocket存在的问题
JxtaMulticastSocket覆写了java.net.MulticastSocket的bind方法: @Override public void bind(SocketAddress addr ...
- xml velocity模板
. <?xml version="1.0" encoding="GBK"?> <PACKET type="REQUEST" ...
- 创建svn仓库脚本
#!/bin/bash svnadmin create $1 cp /home/svndata/conf/svnserver.conf /home/svndata/$1/conf/svnserve.c ...
- 什么是IntelAMT
IntelAMT 全称为INTEL主动管理技术,该技术允许IT经理们远程管理和修复联网的计算机系统,而且实施过程是对于服务对象完全透明的,从而节省了用户的时间和计 算机维护成本.释放出来的iAMT构架 ...
- HDU 5794 A Simple Chess (Lucas + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题. 题目让你求一个棋子开始在( ...
- POJ3345
http://poj.org/problem?id=3345 大意: 大意是说现在有n个城市来给你投票,你需要至少拿到m个城市的赞成票.想要获得第i个城市的赞成需要花费w[i],有个条件就是某些城市是 ...
- CodeForces 709C Letters Cyclic Shift (水题)
题意:给定一个字符串,让你把它的一个子串字符都减1,使得总字符串字典序最小. 析:由于这个题是必须要有一个字串,所以你就要注意这个只有一个字符a的情况,其他的就从开始减 1,如果碰到a了就不减了,如果 ...