leetcode@ [30/76] Substring with Concatenation of All Words & Minimum Window Substring (Hashtable, Two Pointers)
2016-02-25 06:04
429 查看
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:
words:
You should return the indices:
(order does not matter).
View Code
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: string minWindow(string s, string t) { if(s.length() == 0 || 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=0; i<t.length(); ++i) { h[t[i]]++; mh[t[i]] = true; } int cnt = t.length(), l = 0, minRange = INT_MAX, mini, minj; for(int r=0; r<s.length(); ++r) { if(mh[s[r]]) { --h[s[r]]; if(h[s[r]] >= 0) --cnt; } if(cnt == 0) { while(!mh[s[l]] || h[s[l]] < 0) { ++h[s[l]]; ++l; } if(minRange > r - l + 1) { minRange = r - l + 1; mini = l; } } } if(minRange == INT_MAX) return ""; return s.substr(mini, minRange); } };
View Code
相关文章推荐
- 处理解决方案中网站名称为副本名称的方法
- Hbase 基本操作
- Android 文件的读写(内置存储卡)
- Connection refused Will not attempt to authenticate using SASL
- table column sum
- DE24 Introduction to First-order Systems of ODEs
- DE23 Use with Impulse Inputs
- DE22 Using Laplace Transform to Solve ODEs with Discontinuous Inputs
- DE21 Convolution Formula
- 搭建 AngularJS+Ionic+Cordova 开发环境并运行一个demo
- 【CSS3】伸缩布局
- foreach 是传入参数是 Set<Long[]>时,数据循环都是同样数据的问题
- eclipse 连接 habse 问题汇总
- [leetcode] 273. Integer to English Words 解题报告
- 文章标题
- 论clone深层复制和浅层复制
- 【CSS3】盒子模型
- Hashtable之哈希表一
- jquery中各个事件执行顺序如下:
- IOS Apps 开发(Swift)(3)——Build a Basic UI(2)