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 ...
随机推荐
- SSH架构简单总结
Struts.spring.Hibernate在各层的作用 1)struts 负责 web层. ActionFormBean 接收网页中表单提交的数据,然后通过Action 进行处理,再Forw ...
- Codeforces Round #243 (Div. 2) A~C
题目链接 A. Sereja and Mugs time limit per test:1 secondmemory limit per test:256 megabytesinput:standar ...
- HDU2896+AC自动机
ac自动机 模板题 /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include&l ...
- 2.Adding a Controller
MVC stands for model-view-controller. MVC is a pattern for developing applications that are well ar ...
- sql快捷键
请柬:https://msdn.microsoft.com/zh-cn/library/ms174205.aspx
- Web开发的绝美网站
http://paranimage.com/ http://sixrevisions.com/graphics-design/
- Spring中的实例生成方式及其生命周期
三种实例化bean的方式1.使用类构造器实例化 <!-- 使用类构造器实例化,class属性表示要使用的类的全限定名 --> <bean id="userDao1" ...
- POJ2965——The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Description The game “The Pilots Brothers: following the stripy el ...
- TSharding:用于蘑菇街交易平台的分库分表组件
tsharding TSharding is the simple sharding component used in mogujie trade platform. 分库分表业界方案 分库分表TS ...
- 分布式搜索Elasticsearch——QueryBuilders.matchPhrasePrefixQuery
注:该文项目基础为分布式搜索Elasticsearch——项目过程(一)和分布式搜索Elasticsearch——项目过程(二),项目骨架可至这里下载. ES源代码中对matchPhrasePrefi ...