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. onClick(View) of type new View.OnClickListener(){} must override a superclass method

    原地址:http://blog.csdn.net/aeolus1019/article/details/8014798 Android开发过程中代码错误报错如下: - implements andro ...

  2. linux 使用kill命令杀死进程的几个办法

    常规篇: 首先,用ps查看进程,方法如下: $ ps -ef ……smx       1822     1  0 11:38 ?        00:00:49 gnome-terminalsmx   ...

  3. 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttribute..ctor” 解决方案

    静态类中添加如下.此方法本人测试有效. //缺少编译器要求的成员“ystem.Runtime.CompilerServices.ExtensionAttribute..ctor” namespace  ...

  4. ANDROID_MARS学习笔记_S01_012_SeekBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  5. 情人节红攻瑰--Delphi版本

    在oschina上看到了用c写的红玫瑰, 以前只见过用js写的, 就随手用delphi翻译了c的代码, 效果还不错哈.... 原c作者jokeym贴子 http://www.oschina.net/c ...

  6. JavaScript代码调试

    怎么在浏览器中调试JavaScript代码呢?首先,你需要安装Google Chrome浏览器,Chrome浏览器对开发者非常友好,可以让你方便地调试JavaScript代码.安装后,随便打开一个网页 ...

  7. SQL数据库还原时备份集中的数据库备份与现有的数据库不同的解决办法

    SQL Server 2005数据库还原出错错误具体信息为:备份集中的数据库备份与现有的A数据库不同 具体操作如下:第一次:新建了数据库A,数据库文件放在E:\DB\A目录下,选中该数据库右键-任务- ...

  8. 退出myeclipse 8.5配置中心

    用myeclipse 8.5没多久,进入软件中心下载插件,找不到退出按钮,唉,木想到就是一图标.

  9. strlen的C/C+++实现

    2013-07-05 11:36:05 小结: 本函数给出了几种strlen的实现,有ugly implementation,也有good implementation.并参考标准库中的impleme ...

  10. 比nerdtree更好的文件浏览器:vimfiler

    通过:VimFilerExplorer来打开一个文件浏览器 h:收起 t:展开 -:close 回车:进入或展开 空格:收起