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)的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  4. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

  5. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  6. 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 ...

  7. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  8. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

  9. [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 ...

随机推荐

  1. 快速配置Ehcache

    1. 编写ehcache.xml文件,将该文件放置于classpath路径下.代码如下: <?xml version="1.0" encoding="UTF-8&q ...

  2. python参考手册--第9章

    1.读取命令行选项 (1)sys.args python启动时,命令行选项设置在列表sys.args中. sys.args[0]:xxx.py sys.args[1:]: 其他参数 (2)optpar ...

  3. Pie Charts

    Default pie chart   The default pie chart with no options set. Source Code $.plot('#placeholder', da ...

  4. 漫话C++0x(五)—- thread, mutex, condition_variable

    熟悉C++98的朋友,应该都知道,在C++98中没有thread, mutex, condition_variable这些与concurrency相关的特性支持,如果需要写多线程相关程序,都要借助于不 ...

  5. JavaScript 三种创建对象的方法

    JavaScript中对象的创建有以下几种方式: (1)使用内置对象 (2)使用JSON符号 (3)自定义对象构造 一.使用内置对象 JavaScript可用的内置对象可分为两种: 1,JavaScr ...

  6. cocos2d-x3.9 NDK android 环境搭建过程中遇到的错误

    编译环境:Mac OS, NDK r9d 错误:arm-linux-androideabi-gcc: error trying to exec '/media/Project/adt-bundle-l ...

  7. tomcat集群部署

    1.apache只有处理静态事物的能力, 而tomcat的强项就是处理动态的请求 2.由apache作为入口,如果是请求静态页面或者是静态文件,由apache直接提供,如果是请求动态页面,则让apac ...

  8. 201. Bitwise AND of Numbers Range

    题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...

  9. (转)CSS+DIV float 定位

    来自:http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html 很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能 ...

  10. ASP.NET树形控件TreeView的递归绑定

    来自:http://blog.csdn.net/xqf003/article/details/4958727