博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Minimum Window Substring
阅读量:6975 次
发布时间:2019-06-27

本文共 4296 字,大约阅读时间需要 14 分钟。

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 emtpy string "".If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

难度:90    String问题里面有很多不好做的,动不动就DP什么的,参考了一些资料

For example,

S = “ADOBECODEBANC”
T = “ABC”
Minimum window is “BANC”.

Thoughts:

The idea is from . I try to rephrase it a little bit here. The general idea is that we find a window first, not necessarily the minimum, but it’s the first one we could find, traveling from the beginning of S. We could easily do this by keeping a count of the target characters we have found. After finding an candidate solution, we try to optimize it. We do this by going forward in S and trying to see if we could replace the first character of our candidate. If we find one, we then find a new candidate and we update our knowledge about the minimum. We keep doing this until we reach the end of S. For the giving example:

    1. We start with our very first window: “ADOBEC”, windowSize = 6. We now have “A”:1, “B”:1, “C”:1 (保存在needToFind数组里)
    2. We skip the following character “ODE” since none of them is in our target T. We then see another “B” so we update “B”:2. Our candidate solution starts with an “A” so getting another “B” cannot make us a “trade”. (体现在代码就是只有满足hasFound[S.charAt(start)] > needToFind[S.charAt(start)]) 才能移动左指针start)
    3. We then see another “A” so we update “A”:2. Now we have two “A”s and we know we only need 1. If we keep the new position of this “A” and disregard the old one, we could move forward of our starting position of window. We move from A->D->O->B. Can we keep moving? Yes, since we know we have 2 “B”s so we can also disregard this one. So keep moving until we hit “C”: we only have 1 “C” so we have to stop. Therefore, we have a new candidate solution, “CODEBA”. Our new map is updated to “A”:1, “B”:1, “C”:1.
    4. We skip the next “N” (这里忽略所有不在T的字符:用needToFind[S.charAt(start)] == 0来判断) and we arrive at “C”. Now we have two “C”s so we can move forward the starting position of last candidate: we move along this path C->O->D->E until we hit “B”. We only have one “B” so we have to stop. We have yet another new candidate, “BANC”.
    5. We have hit the end of S so we just output our best candidate, which is “BANC”.

底下这个做法看似简单,其实里面各种精巧的设计啊:先找到满足条件的一个window(不一定是最优),每次移动右窗口,吸纳一个新元素进去,如果不是目标元素就跳过continue,如果是的话,hasFound数组对应位置值+1,然后看能不能优化窗口大小 by 看能不能移动左窗口,移动条件就是:始终保证窗口里面含有一个T的所有必要元素(个数要保证),直到移不动为止(再移就无法保证一个完整T的各元素个数了),这时就找到一个新的window,看是否最优。

1 public class Solution { 2     public String minWindow(String S, String T) { 3         int[] hasFound = new int[256]; 4         int[] needtoFind = new int[256]; 5         for (int i=0; i
needtoFind[S.charAt(start)]) {22 if (hasFound[S.charAt(start)] > needtoFind[S.charAt(start)]) {23 hasFound[S.charAt(start)]--;24 }25 start++;26 }27 if (end-start+1 < minWinSize) {28 minWinSize = end - start + 1;29 minWindow = S.substring(start, end+1);30 }31 }32 }33 return minWindow;34 }35 }

在处理字符串时候用数组比Hashtable要来的方便, 当然这道题也可用HashMap来做:

1 public class Solution { 2     public String minWindow(String S, String T) { 3         if (S==null || T==null || S.length()
target = new HashMap
(); 5 HashMap
real = new HashMap
(); 6 for (int m=0; m
i-left+1) {43 minWin = S.substring(left, i+1);44 minWsize = i-left+1;45 }46 }47 }48 return minWin;49 }50 }

Here's a 10 line template.

1 string minWindow(string s, string t) { 2         vector
map(128,0); 3 for(auto c: t) map[c]++; 4 int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0; 5 while(end
0) counter--; //in t 7 while(counter==0){ //valid 8 if(end-begin

 

转载地址:http://ygesl.baihongyu.com/

你可能感兴趣的文章
怎样自动生成makefile
查看>>
Windows2008R2 AD降级错误解决方案
查看>>
datagridview的数据库设计与使用
查看>>
资源管理器的学习笔记一
查看>>
JAVA中写时复制(Copy-On-Write)Map实现
查看>>
推荐12个漂亮的 CSS3 按钮实现方案
查看>>
顺序栈
查看>>
[译] OpenStack Kilo 版本中 Neutron 的新变化
查看>>
Lintcode: Sqrt(X)
查看>>
iOS:UIPageViewController翻页控制器控件详细介绍
查看>>
网站推广优化教程100条(SEO,网站关键字优化,怎么优化网站,如何优化网站关键字)...
查看>>
SQLServer游标(Cursor)简介和使用说明
查看>>
iOS: ios视频播放(MPMediaPlayerController,AVPlayer,AVPlayerViewcontroller、ffmpeg-AVPlayer)...
查看>>
asp.net中实现登陆的时候用SSL
查看>>
Git常用命令总结【转】
查看>>
PostgreSQL中的AnyEnum例子
查看>>
Delphi 中的 XMLDocument 类详解(14) - 遍历 XML 文件
查看>>
《gradle 用户指南》中文版 第2章 概述
查看>>
Dockerfile指令
查看>>
FFmpeg 学习资料
查看>>