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

Example:

Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

Note:

  • If there is no such window in S that covers all characters in T, return the empty string "".
  • If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

题意:

给定字符串S 和 T, 求S中可以cover T所有元素的子集的最小长度。

Solution1: Two Pointers(sliding window)

1.  scan String T,  using a map to record each char's frequency

2.  use [leftMost to i] to maintain a sliding window, making sure that each char's frequency in such sliding window == that in T

3.  if mapS [S.charAt(start)]  >  mapT [S.charAt(start)] ,  it signs we can move sliding window

4.  how to find the next sliding window?  move leftMost, meanwhile,  decrement mapS [S.charAt(start)]  until we find each frequency in  [start to i] == that in T

code

 class Solution {
public String minWindow(String S, String T) {
String result = "";
if (S == null || S.length() == 0) return result;
int[] mapT = new int[256];
int[] mapS = new int[256];
int count = 0;
int leftMost = 0;
for(int i = 0; i < T.length(); i++){
mapT[T.charAt(i)] ++;
} for(int i = 0; i < S.length(); i++){
char s = S.charAt(i);
mapS[s]++;
if(mapT[s] >= mapS[s]){
count ++;
} if(count == T.length()){
while(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]){
if(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]){
mapS[S.charAt(leftMost)]--;
}
leftMost ++;
}
if(result.equals("") || i - leftMost + 1 < result.length()){
result = S.substring(leftMost, i+1);
}
}
}
return result;
}
}

二刷:

对于出现在S但不出现在T的那些“配角” character的处理,

最好的方式是,边扫S边用map将其频率一并记上。

这样,在判断 while(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]) 这个逻辑的时候,

这些“配角”character会因为只出现在S但不出现在T

而直接被left++给做掉

 class Solution {
public String minWindow(String s, String t) {
String result = "";
if(s == null || s.length() == 0 || s.length() < t.length()) return result; int [] mapT = new int [128];
for(Character c : t.toCharArray()){
mapT[c]++;
} int left = 0;
int count = t.length();
int[] mapS = new int[128];
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
mapS[c] ++ ;
if(mapT[c] >= mapS[c]){
count --;
}
if(count == 0){
while(mapS[s.charAt(left)] > mapT[s.charAt(left)]){
mapS[s.charAt(left)] --;
left++;
}
if (result.equals("") || i - start + 1 < result.length()) {
result = s.substring(start, i + 1);
}
}
}
return result;
}
}

最新文章

  1. (转帖)开源容器集群管理系统Kubernetes架构及组件介绍
  2. Entity Framework 中使用SQL Server全文索引(Full Text Search)
  3. React学习笔记-1-什么是react,react环境搭建以及第一个react实例
  4. idea 小技巧
  5. 让Windows7运行速度更快的BIOS优化设置教程
  6. redis maxmemory设置
  7. android内存优化之图片压缩和缓存
  8. oracle常见错误类型
  9. PAT (Advanced Level) 1068. Find More Coins (30)
  10. 【Owin 学习系列】2. Owin Startup 类解析
  11. 原生ajax的请求函数
  12. Oracle dblink详解
  13. 初次尝试使用jenkins+python+appium构建自动化测试
  14. Bug预防体系(上千bug分析后总结的最佳实践)
  15. 纯css使用线性渐变实现滚动进度条(来自于微信前端早读课)
  16. Linux 系统查看对应公网映射地址
  17. 【LeetCode】7. 整数反转
  18. Network-Emulator&#160;Network-Emulator-Toolkit网络模拟器使用详细介绍
  19. GIT 分支管理:分支管理策略、Bug分支、Feature分支
  20. oracle PL/SQL的介绍

热门文章

  1. JDK8安装与配置
  2. nginx+keeplived+tomcat
  3. Linux之find
  4. Linux系统挂载新磁盘
  5. SqlServer查询某个表的列名称、说明、备注、类型等
  6. [UE4]RichTextBlock
  7. Pillow《转载》
  8. tkinter 写一个简易的ide
  9. day38数据库MySQL基础
  10. WPF 去掉Drag a column header here to group by that column