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 ...
随机推荐
- Eclipse 插件开发 —— 深入理解查找(Search)功能及其扩展点
引言 查找功能是计算机语言开发环境 / 平台的一个非常重要的特性.Eclipse 也不例外,它提供了丰富的查找功能(用户可以输入正则表达式或任意字符串,指定查找范围和匹配选项等等),并且提供了简单易用 ...
- android 使用系统照相程序照相并存储、显示在界面上
大部分业务可以通过调用系统的相机程序来拍照. 界面如下: <?xml version="1.0" encoding="utf-8"?> <Li ...
- 手机金属外壳加工工艺:铸造、锻造、冲压、CNC
现如今金属手机成为行业的热点,在消费电子产品中应用越来越广,本文详细介绍几种金属加工工艺及相关产品应用. 1.CNC+阳极:iPhone 5/6, HTC M7 2.锻造+CNC:华为P8,HTC M ...
- 深入php面向对象和模式
前两章是php历史和概论,略过. 第三章 对象基础 3.1 类和对象 类,是用于生成对象的代码模版. public 公有的,都可调用. protected 保护的, 只有本类和子类可以调用. priv ...
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...
- Hoax or what
Hoax or what 题意是询问一个动态序列的最小值和最大值. 可以用multiset来实现. #include <stdio.h> #include <set> usin ...
- eay ui iframe 下常问题
背景:客户要使用https进行登录,把原来的登录做到一个小框,用iframe嵌进来进行登录. 客户拥有4个域名,但只在xxx.com域名中购买了安全证书,所以多个域名下登录所用的iframe中src是 ...
- window下gvim中文界面改变成英文界面
中文环境下设置GVIM的界面.菜单.提示为英文语言 修改你的_vimrc,通常为类似C:\Program Files\Vim 加入以下语句至末尾 " set the menu & t ...
- 【转】Ubuntu乱码解决方案(全)
转自:http://www.cnblogs.com/end/archive/2011/04/19/2021507.html ubuntu下中文乱码解决方案(全) 1.ibus输入法 Ubuntu 系统 ...
- C# winform DataGridView
C# DataGridView控件动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如 ...