LeetCode OJ-- Substring with Concatenation of All Words ***
https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/
找S中子串,每个元素都在T中出现了,且所有T中元素都在S子串中出现了
class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
vector<int> ans;
if(S.empty() || S.size() == || L.empty() || L.size() == )
return ans;
// L is longer
int len = L.size() * L[].size();
if(len > S.size())
return ans;
unordered_map<string,int> dictL;
for(int i = ; i< L.size(); i++)
dictL[L[i]] += ;
unordered_map<string,int> _dict;
for(int i = ; i + len <= S.size(); i++)
{
string subS = S.substr(i,len);
_dict.clear();
bool flag = true;
for(int j = ; j < len; j+= L[].size())
{
string subLittle = subS.substr(j,L[].size());
if(dictL.find(subLittle) == dictL.end()) //不属于目标里面的
{
flag = false;
break;
}
_dict[subLittle] += ;
if(_dict[subLittle] > dictL[subLittle])
{
flag = false;
break;
}
}
if(flag)
ans.push_back(i);
}
return ans;
}
};
超时、超时、超时、超时……
教训就是,有时候超时不是算法的问题,而是实现的不好。具体看下一篇博客
LeetCode OJ-- Substring with Concatenation of All Words ***的更多相关文章
- [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 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】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 030 Substring with Concatenation of All Words
题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- spark 的createDstream和createDirectStream区别
spark读取kafka数据流提供了两种方式createDstream和createDirectStream. 两者区别如下: 1.KafkaUtils.createDstream 构造函数为Kafk ...
- 洛谷P1067 多项式输出
题目链接:https://www.luogu.org/problemnew/show/P1067 这是一个纯模拟的小怪但是需要注意一些小细节: 1.首项为正没有+号. 2.所有项系数如果是一的话就省略 ...
- luogu3195 [HNOI2008]玩具装箱TOY
懒得写 #include <iostream> #include <cstdio> using namespace std; typedef long long longliv ...
- DEDE调用指定文章ID来调用特定文档
http://www.jb51.net/cms/137423.html 代码如下: {dede:arclist row=1 idlist='6'} <li><a href=" ...
- MSSQL将多行单列变一行一列并用指定分隔符分隔,模拟Mysql中的group_concat
-- 将多行记录(只能一个列)用指定分隔符分隔 IF(OBJECT_ID('sp_RowsChangeClosBySplit',N'P') IS NOT NULL) DROP PROC sp_Rows ...
- Careercup - Microsoft面试题 - 5672369481842688
2014-05-12 06:27 题目链接 原题: Find the max height of a binary tree. 题目:计算二叉树的最大高度. 解法:最大高度?高度不就是最深的叶子节点到 ...
- leetcode 【 Minimum Path Sum 】python 实现
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- redhat--1
---------------- ---------------- 免密码ssh远程登录设置 . In host1, copy the ssh-key to the host2 hosts # ssh ...
- web自动化测试:watir+minitest(一)
基本介绍: 本课程的测试环境和工具为:win7+ruby+watir+minitest Watir 全称是"Web Application Testing in Ruby".它是一 ...
- Ubuntu安装opencv with cuda
Ubuntu安装opencv with cuda 为了运行dense flow真是折腾啊,下面网址是教程 http://blog.aicry.com/ubuntu-14-04-install-open ...