【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 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).
我的思路,
设S的长度为Slen, L的长度为Llen, L中每个单词的长度为Lstrlen
把L中的单词排序
首个字符遍历S中的每一个字符
然后以首字符为起点,获取Llen个Lstrlen长度的单词,把获取的单词排序。
比较两个排序后的单词是否相同。相同则压入答案,不同则遍历下一个起点。
时间复杂度为O(Slen*Llen*log(Llen))
结果超时了。分析一下原因:
S: "barfoothefoobarman"
L: ["foo", "bar"]
在遍历第一个起始点时,我们获取了 bar foo
在遍历第4个起始点时,我们获取了 foo the. 但这个foo在遍历第一个起始点时已经遍历过了,故我们做了重复的计算。
------------------------------------------------------------------------------------------------------
下面是大神O(Slen*Lstrlen)的答案
用hash表cn记录每个单词出现的次数
对 i = 0 - Lstrlen 遍历
对 j = i - Slen遍历 ;j+= Lstrlen
获取以j为起点的一个单词。
if(不存在这个单词)
起始位置订到下一个单词起始点,数据清0
else if(存在这个单词,并且出现次数小于cn中统计的次数)
获取的单词数增加
else(存在这个单词,但出现次数大于等于cn中统计的次数)
把起始位置重新定位到这个单词第一次出现之后
if(获取单词数==L中单词数)
压入答案
class Solution {
public:
//超时
vector<int> findSubstring2(string S, vector<string> &L) {
vector<int> ans;
if(L.empty()) //L是空的
{
return ans;
}
int Llen = L.size();
int Slen = S.size();
int Lstrlen = L[].size();
if(Slen < Lstrlen) //S没有L中的字符串长
{
return ans;
}
sort(L.begin(), L.end());
for(int i = ; i < Slen - Llen * Lstrlen; i++)//起始位置循环
{
bool isOK = true;
vector<string> cur;
for(int j = ; j < Llen; j++)
{
cur.push_back(S.substr(i + j * Lstrlen, Lstrlen));
}
sort(cur.begin(), cur.end());
for(int j = ; j < Llen; j++)
{
if(cur[j] != L[j])
{
isOK = false;
break;
}
}
if(isOK)
{
ans.push_back(i);
i += Llen * Lstrlen - ;
}
}
return ans;
}
};
//大神可以通过的代码
class Solution2 {
private:
vector<int> res;
map<string,int> cntL;
map<string,int> cn;
int n ;
public:
vector<int> findSubstring(string S, vector<string> &L)
{ res.clear();
cntL.clear();
cn.clear();
n = S.length();
int e = L.size();
int t = L[].length();
int k = ;
for(int i = ; i < e ; i++)
{ if(cn.count(L[i]) == )
{ cn[L[i]] = ;
k++;
}
else
{ cn[L[i]] += ;
k++;
}
}
string tr ,du;
int r = ;
int st = ;
for(int j = ; j < t ; j++)
{ r = ; st = j;
for(int i = j; i < n; i += t)
{ tr = S.substr(i,t);
if( cn.count(tr) == || cn[tr] == )
{ cntL.clear();
r = ;
st = i+t;
}
else if(cntL[tr] < cn[tr])
{ cntL[tr] += ;
r++;
}
else
{ du = S.substr(st,t);
while(du != tr)
{ cntL[du]--;
r--;
st += t;
du = S.substr(st,t);
}
st += t;
}
if(r == k)
{ res.push_back(st);
du = S.substr(st,t);
cntL[du]--;
r--;
st += t;
}
}
cntL.clear();
}
sort(res.begin(),res.end());
return res ;
}
};
【leetcode】Substring with Concatenation of All Words (hard) ★的更多相关文章
- 【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】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- 2015年11月26日 Java基础系列(四)class的定义,继承和实现interface
序,类的设计是JAVA操作的核心,面对对象思想中一切皆对象. 一.类定义中的变量 静态成员变量,为类所有,称为类变量:只有一份,编译时即分配值,使用关键字static声明. 非静态成员变量,每个实例一 ...
- CSS3的REM设置字体大小
在Web中使用什么单位来定义页面的字体大小,至今天为止都还在激烈的争论着,有人说PX做为单位好,有人说EM优点多,还有人在说百分比方便,以至于出现了CSS Font-Size: em vs. px v ...
- 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.ServiceModel.Activation.HttpModule”。
********************************* 思路壹(也是网络上铺天盖地的通俗解决方案) 原因: 这是因为先安装了 .NET Framework , 随后启用了 .NET Fra ...
- MySQL性能优化的21条最佳经验【转】
转载自http://www.cnblogs.com/jiaosq/p/5843437.html 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只 ...
- Redis学习笔记五:独立功能之事务
Redis 事务提供了一种将多个命令请求打包,然后一次性.按顺序地执行多个命令的机制,并且在事务执行期间,服务器不会中断,会将事务中的所以命令都执行完毕才去处理其他客户端的命令请求. 事务的实现 事务 ...
- iOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档
1.iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2.应用沙盒每 ...
- Android学习笔记(十)——ListView的使用(上)
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! ListView绝对可以称得上是 Android中最常用的控件之一,ListView允许用户通过手指上下滑动的 ...
- 学习Node.js笔记(一)
一.什么是Node.js 1.1.Node.js是什么 Node.js是用来编写高性能网络服务器的JavaScript工具包 Node.js 是一个基于Chrome JavaScript 运行时建立的 ...
- silk与opencore-amr音频编码对比
silk与opencore-amr编码对比 在采样率8000 单声道 16位采样精度情况下 silk的压缩率为 1/15 opencore-amr 1/17 对比图 原始的音频编码 opencore- ...
- js 函数前的+号
不知啥时候起,函数的闭包需要增加+才能立即执行了. 不加反而报语法错.orz +function() { console.log("Foo!"); }(); 输出: Foo!< ...