给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符。
示例:
S = "ADOBECODEBANC"
T = "ABC"
最小窗口是 "BANC".
注意事项:
如果 S 中没有覆盖 T 中所有字符的窗口,则返回空字符串 ""。
如果有多个这样的窗口,你将会被保证在 S 中总是只有一个唯一的最小窗口。
详见:https://leetcode.com/problems/minimum-window-substring/description/

Java实现:

class Solution {
public String minWindow(String s, String t) {
if (s == null || t == null || s.length() < t.length()){
return "";
}
// HashMap的key为t中各个字符,value为对应字符个数
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : t.toCharArray()) {
if (!map.containsKey(c)){
map.put(c, 0);
}
map.put(c, map.get(c) + 1);
}
// minLeft为最小窗口左下标,minLen为最小长度,count用来计数
int minLeft = 0, minLen = s.length() + 1, count = 0;
int left = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
// 如果map.get(c)说明t中还有字符没有包含,计数器+1
if (map.get(c) > 0){
count++;
}
map.put(c, map.get(c) - 1);
}
// 如果left到i中包含t中所有字符
while (count == t.length()) {
if (i - left + 1 < minLen) {
minLeft = left;
minLen = i - left + 1;
}
c = s.charAt(left);
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
if (map.get(c) > 0){
count--;
}
}
left++;
}
}
if (minLen > s.length()){
return "";
} return s.substring(minLeft, minLeft + minLen);
}
}

参考:https://www.nowcoder.com/questionTerminal/c466d480d20c4c7c9d322d12ca7955ac

详见:https://www.cnblogs.com/grandyang/p/4340948.html

076 Minimum Window Substring 最小窗口子字符串的更多相关文章

  1. Java for LeetCode 076 Minimum Window Substring

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

  2. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

  3. Minimum Window Substring @LeetCode

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

  4. [Swift]LeetCode76. 最小覆盖子串 | 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

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  6. 刷题76. Minimum Window Substring

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

  7. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  8. LeetCode(76) Minimum Window Substring

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

  9. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

    1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...

随机推荐

  1. 破解 Navicat Premium 12

    一.下载 若文件百度云链接失效,请发邮件给博主:1766211120@qq.com 1.安装文件下载 v12.0.11(x64)版本下载地址如下 链接:https://pan.baidu.com/s/ ...

  2. wordpress汇总(持续更新)

    在wordpress上新建编辑了几个页面,总是不能正常发布预览.经调查是由于固定链接的设置有问题导致的.打开左侧栏目“设置”中的固定链接项,可以看到目前所选的是“自定义结构”型.将其更改为“朴素”型后 ...

  3. codeforces 706C C. Hard problem(dp)

    题目链接: C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. css 3d box 实现的一些注意事项

    Test1.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  5. BZOJ1758:[WC2010]重建计划

    浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...

  6. 学习总结:斯特林数( Stirling number )

    基本定义 第一类斯特林数:$1 \dots n$的排列中恰好有$k$个环的个数:或是,$n$元置换可分解为$k$个独立的轮换的个数.记作 $$ \begin{bmatrix} n \\ k \end{ ...

  7. sorted matrix - search & find-k-th

    sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right ...

  8. VMware10中安装centos7没有可用的网络设备

    1.问题描述 centos7安装到虚拟机无法上网   2.安装环境 win7 x64  WM 10.01 iso  CentOS-7-x86_64-DVD-1503-01.iso  {4.01G}   ...

  9. 9. 那些强悍的PHP一句话后门

    强悍的PHP一句话后门 这类后门让网站.服务器管理员很是头疼,经常要换着方法进行各种检测,而很多新出现的编写技术,用普通的检测方法是没法发现并处理的.今天我们细数一些有意思的PHP一句话木马. 利用4 ...

  10. Laravel框架的一些配置

    服务器的配置 1.在apache下的配置 配置httpd-conf:php5_module.rewrite_module.Listen 配置extra/httpd-vhost:端口.站点.域名.默认首 ...