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.

重点:

  • 滑动窗口
  • Map的put,get,判断是否存在,遍历
  • String的截取
class Solution {
public String minWindow(String s, String t) {
Map<Character,Integer> target = new HashMap<Character,Integer>();
for(int i = 0; i < t.length(); i++){
if(!target.containsKey(t.charAt(i))) target.put(t.charAt(i),1);
else target.put(t.charAt(i), target.get(t.charAt(i))+1);
} int left = 0;
int right = 0;
int minLength = Integer.MAX_VALUE;
int minLeft = 0;
int minRight = s.length()-1;
Map<Character,Integer> source = new HashMap<Character,Integer>();
source.put(s.charAt(0),1);
while(left<=right){
if(ifContain(source,target)){
if(right-left+1 < minLength){
minLength = right-left+1;
minLeft = left;
minRight = right;
} source.put(s.charAt(left), source.get(s.charAt(left))-1);
left++;
}
else{
right++;
if(right == s.length()) break; if(!source.containsKey(s.charAt(right))) source.put(s.charAt(right),1);
else source.put(s.charAt(right), source.get(s.charAt(right))+1);
}
} if(minLength==Integer.MAX_VALUE) return "";
else return s.substring(minLeft,minRight+1);
} public Boolean ifContain(Map<Character, Integer> source, Map<Character, Integer> target){
for(Character key: target.keySet()){
if(!source.containsKey(key) || source.get(key) < target.get(key)) return false;
}
return true;
}
}

76. Minimum Window Substring (JAVA)的更多相关文章

  1. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  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. 76. Minimum Window Substring

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

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 76. Minimum Window Substring(hard 双指针)

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. [LC] 76. Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. 【一天一道LeetCode】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. 《信息安全系统设计基础》--Myod

    Myod 回顾Makefile 任务详情 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 main与其他分开,制作静态库和动态库 编写Mak ...

  2. Miniui 表单验证

    自定义表单验证: input输入框的表单验证可通过vtype和onvalidation事件两种方式实现 可编辑列表(例如div)的表单验证只能通过vtye来实现表单验证 (1)vtype方式: jsp ...

  3. SpringBoot 2.x 使用Redis作为项目数据缓存

    一.添加依赖 <!-- 添加缓存支持 --> <dependency> <groupId>org.springframework.boot</groupId& ...

  4. 最近给几个CRM软件配套开发了Outlook插件,讲讲Outlook插件开发注意事项

    原始出处:www.cnblogs.com/Charltsing/p/OutlookAddinsTips.html联系QQ:564955427 从去年到现在,写了四五个Outlook插件,其中两个是给C ...

  5. leetcode 1两数之和

    使用哈希的方法:先将nums哈希表化,再遍历nums,寻找-nums[i]如果存在则为题目所求 class Solution { public: vector<int> twoSum(ve ...

  6. Ubantu下部署Flask项目安装与配置

    1.nginx 安装 sudo apt-get install nginx 启动,停止和重启 sudo /etc/init.d/nginx start sudo /etc/init.d/nginx s ...

  7. 使用 Python* 的英特尔® 分发版实现 Unity* 机器学习入门(第 1 部分)

    本文将向游戏开发人员介绍如何使用强化学习创建更好的人工智能 (AI) 行为.使用Python* 的英特尔® 分发版 — 常用面向对象的高级编程语言的进阶版 — 读者可收集关于如何训练预先存在的机器语言 ...

  8. 手机app打开的web,在打开chrome浏览器

    手机app打开的web在,打开chrome浏览器 <a href='intent://#Intent;action=android.intent.action.VIEW;scheme=googl ...

  9. 【安卓开发】一个简单快递查询APP实例的实现摘要

    前言 做毕业设计涉及到安卓开发,决定好好学习安卓开发.在正式做毕业设计之前,有必要先设计和完成一个与毕业设计最终成果相关的demo或者说样例APP.最终毕业设计需要实现的功能包括通过调用PHP端API ...

  10. 深入理解java:2.4. 线程本地变量 java.lang.ThreadLocal类

    ThreadLocal,很多人都叫它做线程本地变量,也有些地方叫做线程本地存储,其实意思差不多. 可能很多朋友都知道ThreadLocal为变量在每个线程中都创建了一个副本,那样每个线程可以访问自己内 ...