76-Minimum Window Substring

标签: 算法 数据结构 leetcode
13人阅读 评论(0) 收藏 举报
分类:

Description:

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和T,在O(n)的时间复杂度下找出S中包含T所有字符的最小窗口。
例如,S = “ADOBECODEBANC”, T = “ABC”, 返回”BANC”


问题分析
注意题目要求是包含所有字符就可以了,不需要注意顺序
这种题下面这个链接总结的模板非常好,建议看一下:
https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-‘substring’-problems


解法

class Solution {
    public String minWindow(String s, String t) {
        int[] map = new int[128];

        for(char c : t.toCharArray())  map[c]++;

        int counter = t.length(), start = 0, end = 0, d = Integer.MAX_VALUE, minstart = 0;

        while(end < s.length()){
            char c = s.charAt(end);
            if(map[c] > 0)  counter--;
            map[c]--;
            end++;
            while(counter == 0){
                if(end - start < d){
                    d = end - start;
                    minstart = start;
                }
                char c1 = s.charAt(start);
                if(map[c1] == 0)    counter++;
                map[c1]++;
                start++;
            }
        }

        return d == Integer.MAX_VALUE ? "" : s.substring(minstart, minstart + d);
    }
}
查看评论

Minimum Window Substring -- LeetCode

原题链接: http://oj.leetcode.com/problems/minimum-window-substring/  这道题是字符串处理的题目,和Substring with Conca...
  • linhuanmars
  • linhuanmars
  • 2014-03-03 04:23:50
  • 22171

LeetCode --- 76. Minimum Window Substring

题目链接:Minimum Window Substring Given a string S and a string T, find the minimum window in S which w...
  • makuiyu
  • makuiyu
  • 2015-03-23 21:26:09
  • 2162

LeetCode76——Minimum Window Substring

LeetCode76——Minimum Window Substring 以前似乎做过类似的,点了LeetCode类似题想起来了(这点把不同类型的题目分类真心不错) 好了,跟LeetCode30——S...
  • zhangxiao93
  • zhangxiao93
  • 2015-11-17 20:59:57
  • 1526

[leetcode-76]Minimum Window Substring(java)

问题描述: Given a string S and a string T, find the minimum window in S which will contain all the char...
  • zdavb
  • zdavb
  • 2015-08-05 20:28:08
  • 562

leetcode -- Minimum Window Substring -- 重点,应该会考

https://leetcode.com/problems/minimum-window-substring/思路: 解题思路:双指针思想,尾指针不断往后扫,当扫到有一个窗口包含了所有T的字符,然后...
  • xyqzki
  • xyqzki
  • 2015-12-22 21:31:45
  • 486

LeetCode 76. Minimum Window Substring(最小窗口子串)

原题网址:https://leetcode.com/problems/minimum-window-substring/ Given a string S and a string T, find...
  • jmspan
  • jmspan
  • 2016-05-22 05:19:30
  • 322

LeetCode(76)Minimum Window Substring

题目如下: Given a string S and a string T, find the minimum window in S which will contain all the chara...
  • feliciafay
  • feliciafay
  • 2015-03-22 08:05:39
  • 1875

[Leetcode]76. Minimum Window Substring @python

题目Given a string S and a string T, find the minimum window in S which will contain all the character...
  • qian2729
  • qian2729
  • 2016-01-21 21:52:24
  • 457

[LeetCode][Java] Minimum Window Substring

题目: Given a string S and a string T, find the minimum window in S which will contain all the ch...
  • Evan123mg
  • Evan123mg
  • 2015-07-18 10:34:09
  • 809

Minimum Window Substring 最小覆盖子串算法

题目  最小子串覆盖  描述 笔记  数据  评测 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串。 您在真...
  • fly_yr
  • fly_yr
  • 2016-04-12 16:13:57
  • 3683
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 2万+
    积分: 6531
    排名: 4582
    博客专栏
    文章存档
    最新评论