题目:

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.

思路:举个例子吧

S="aabcdafac"   T="ac"

保存两个指针,分别指向当前已找到的字符串的start和end位置;我们先找到了aabc,这是第一个包含ac的字符串,好的,这个长度为4,start=0,end=3,这时,保持end不动,移动start,发现start=1,end=3可以更短。之后再就不能移start了。

接下来,咱们移动end,发现移到end=4,不是a或c,那就再移,移到end=5,发现是a,这时start还是1,咱们移动start,发现可以一直移到start=3。

接下来再移动end,如此类推

结合代码走一走,就会发现这个思路还是很线性的。

package string;import java.util.HashMap;
import java.util.Map;public class MinimumWindowSubstring {public String minWindow(String s, String t) {int m = s.length();int n = t.length();Map<Character, Integer> found = new HashMap<Character, Integer>();Map<Character, Integer> base = new HashMap<Character, Integer>();for (int i = 0; i < n; ++i) {char c = t.charAt(i);if (base.containsKey(c)) {base.put(c, base.get(c) + 1);} else {base.put(c, 1);found.put(c, 0);}}int count = 0;int ss = -1;int ee = m;for (int end = 0, start = 0; end < m; ++end) {char c = s.charAt(end);if (base.containsKey(c)) {found.put(c, found.get(c) + 1);if (found.get(c) <= base.get(c))++count;if (count == n) {char startChar = s.charAt(start);while (!base.containsKey(startChar) || found.get(startChar) > base.get(startChar)) {if (base.containsKey(startChar))found.put(startChar, found.get(startChar) - 1);startChar = s.charAt(++start);}if (end - start < ee - ss) {ee = end;ss = start;}}}}return ss == -1 ? "" : s.substring(ss, ee + 1);}public static void main(String[] args) {// TODO Auto-generated method stubString S = "ADOBECODEBANC";String T = "ABC";MinimumWindowSubstring m = new MinimumWindowSubstring();System.out.println(m.minWindow(S, T));}}

转载于:https://www.cnblogs.com/null00/p/5094630.html

LeetCode - Minimum Window Substring相关推荐

  1. [LeetCode] Minimum Window Substring 散列映射问题

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  2. Minimum Window Substring @LeetCode

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

  3. 【打印代码+好好理解+子串问题】LeetCode 76. Minimum Window Substring

    LeetCode 76. Minimum Window Substring 字符串子串问题!!!理解这题真是花了很大的功夫!!! 参考链接:https://blog.csdn.net/weixin_4 ...

  4. LeetCode 76. Minimum Window Substring / 567. Permutation in String

    76. Minimum Window Substring 典型Sliding Window的问题,维护一个区间,当区间满足要求则进行比较选择较小的字串,重新修改start位置. 思路虽然不难,但是如何 ...

  5. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  6. LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  7. LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  8. 72.Minimum Window Substring(最小子串窗口)

    Level:   Hard 题目描述: Given a string S and a string T, find the minimum window in S which will contain ...

  9. Minimum Window Substring 最小覆盖子串算法

    转载:https://blog.csdn.net/fly_yr/article/details/51134340 题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串s ...

最新文章

  1. lstm timestep一般是多少_请问rnn和lstm中batchsize和timestep的区别是什么?
  2. vmware 12.5.3 linux,vmware12如何安装linux|opensuse42.3系统安装教程
  3. elasticsearch备份恢复(单机集群)
  4. Java学习笔记之 IO包 字节流
  5. Android开发笔记(八十一)屏幕规格适配
  6. python工资一般多少-Python工程师工资多少
  7. java 获取xml 版本号_java读取xml文件字段值
  8. JavaScript表单编程
  9. 美国地名大全(美国城市名称英文、中文)
  10. 百度地图 变黑问题 解决方法
  11. QNX系统终端中同一个进程号显示多次
  12. 设计美好的服务器(6)--SEDA架构笔记
  13. VMWare虚拟机快照技术深入理解
  14. Hacking 开心网(虚拟朋友) with Mechanize
  15. 电机驱动芯片--DRV8824 DRV8825
  16. Android九宫格界面实现点击每个格点击跳转界面
  17. python基础-运算符
  18. 销售经理应具备哪些技能
  19. 如何把“蚂蚁呀嘿”换脸特效用在前端 WebRTC 视频通话中
  20. 系统入门到实战学习某项技术、有问题找“百度“、学习优秀的技术博客、找开源代码等资料

热门文章

  1. python语言入门p-python初学者怎么入门
  2. python编程零基础-编程零基础应当如何开始学习 Python?
  3. 手机上开发python有哪些软件-哪个手机软件有python题库
  4. python下载了怎么打开-下载python后如何启动
  5. python下载大文件-python 大文件
  6. python叫什么语言-Python代码是什么语言
  7. 想学python有什么用-python学来有什么用
  8. 北京python培训班价格-北京Python编程培训多少钱
  9. python上海培训哪里比较好-python培训班上海哪里比较好?
  10. python映射类型-Python基础类型之字典(dict)