leetcode@ [30/76] Substring with Concatenation of All Words & Minimum Window Substring (Hashtable, Two Pointers)
https://leetcode.com/problems/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).
class Solution {
public:
bool func(string s, vector<string>& words, map<string, int>& h) {
int n = words.size();
int each = words[].length(); map<string, int> mh; mh.clear();
int i = ;
for(; i<=s.length()-each; i+=each) {
string sub = s.substr(i, each); if(h.find(sub) != h.end()) {
++mh[sub];
if(mh[sub] > h[sub]) return false;
}
else return false;
} return true;
} vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
int n = words.size();
if(n == ) return res; map<string, int> h;
for(int i=; i<n; ++i) {
if(h.find(words[i]) == h.end()) {
h.insert(make_pair(words[i], ));
}
else {
h[words[i]]++;
}
}
int each = words[].length();
int tot = each * n;
if(s.length() < tot) return res; for(int i = ; i <= s.length() - tot; ++i) {
string sub = s.substr(i, tot);
//cout << sub << endl;
if(func(sub, words, h)) res.push_back(i);
} return res;
}
};
https://leetcode.com/problems/minimum-window-substring/
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC"
.
Note:
If there is no such window in S that covers all characters in T, return the empty string ""
.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
class Solution {
public:
string minWindow(string s, string t) {
if(s.length() == || s.length() < t.length()) return "";
if(s.length() == t.length()) {
if(s == t) return s;
} map<char, int> h; h.clear();
map<char, bool> mh; mh.clear();
for(int i=; i<t.length(); ++i) {
h[t[i]]++;
mh[t[i]] = true;
} int cnt = t.length(), l = , minRange = INT_MAX, mini, minj;
for(int r=; r<s.length(); ++r) {
if(mh[s[r]]) {
--h[s[r]];
if(h[s[r]] >= ) --cnt;
} if(cnt == ) {
while(!mh[s[l]] || h[s[l]] < ) {
++h[s[l]];
++l;
} if(minRange > r - l + ) {
minRange = r - l + ;
mini = l;
}
}
} if(minRange == INT_MAX) return ""; return s.substr(mini, minRange);
}
};
leetcode@ [30/76] Substring with Concatenation of All Words & Minimum Window Substring (Hashtable, Two Pointers)的更多相关文章
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
随机推荐
- jsp request.getParameterValues获取数组值代码示例
tt.jsp <form action="tt2.jsp" method="POST"> <select name="two&quo ...
- Nginx开启Gzip压缩大幅提高页面加载速度(转)
转自:http://www.cnblogs.com/mitang/p/4477220.html 刚刚给博客加了一个500px相册插件,lightbox引入了很多js文件和css文件,页面一下子看起来非 ...
- 5.查找最小的k个元素(数组)
题目: 输入n个整数,输出其中最小的k个,例如输入1,2,3,4,5,6,7,8这8个数,则最小的4个是1,2,3,4(输出不要求有序) 解: 利用快速排序的partition,算导上求第k大数的思想 ...
- Photoshop CS4 启动弹出许可协议
win7:删除 1.C:\Users\All Users\FLEXnet\adobe_00080000_tsf.data WinXP:(c:/Documents and Settings/All Us ...
- 最受欢迎的5个Android ORM框架
在开发Android应用时,保存数据有这么几个方式, 一个是本地保存,一个是放在后台(提供API接口),还有一个是放在开放云服务上(如 SyncAdapter 会是一个不错的选择). 对于第一种方式, ...
- 简单的神经元算法实现(python)
参考python代码如下 #perceptron x=[[1 ,0, 0],[1,0,1],[1, 1, 0],[1, 1, 1],[0,0,1],[0,1,0],[0,1,1],[0,0,0]] y ...
- CentOS 6.4 编译安装Mysql 5.6.14
概述: CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14. 正文: 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server ...
- 【HDOJ】4418 Time travel
1. 题目描述K沿着$0,1,2,\cdots,n-1,n-2,n-3,\cdots,1,$的循环节不断地访问$[0, n-1]$个时光结点.某时刻,时光机故障,这导致K必须持续访问时间结点.故障发生 ...
- Spring-Data-JPA学习
Spring-Data-JPA结构图 网址: http://blog.sina.com.cn/s/blog_667ac0360102ecsf.html
- bzoj1485
首先考虑dp,设f[i,j]表示1~i用过了,期中j个放在偶数位然后转移大家都会 这显然TLE,我们观察这个dp,任意前i个数,无论怎么放,放在奇数位的数的个数一定要大于等于放在偶数位的个数 于是很明 ...