`
hcx2013
  • 浏览: 88940 次
社区版块
存档分类
最新评论

Minimum Window Substring

 
阅读更多

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.

 

public class Solution {
    public String minWindow(String s, String t) {
        if (s == null || t == null || s.length() < t.length()) {
        	return "";
        }
        String res = "";
        int[] map = new int[256];
        for (int i = 0; i < t.length(); i++) {
			map[t.charAt(i)]++;
		}
        int left = 0;
        int right = 0;
        int match = t.length();
        int minLen = Integer.MAX_VALUE;
        while (right != s.length()) {
        	map[s.charAt(right)]--;
        	if (map[s.charAt(right)] >= 0) {
        		match--;
        	}
        	if (match == 0) {
        		while (map[s.charAt(left)] < 0) {
        			map[s.charAt(left++)]++;
        		}
        		if (minLen > right - left + 1) {
        			minLen = right - left + 1;
        			res = s.substring(left, right+1);
        		}
        		match++;
        		map[s.charAt(left++)]++;
        	}
        	right++;
        }
        return res;
    }
}

 

 

1
3
分享到:
评论

相关推荐

    leetcode76-LeetCode76_MinimumWindowSubstring:LeetCode76_MinimumWindowSu

    标题 "LeetCode76-LeetCode76_MinimumWindowSubstring:LeetCode76_MinimumWindowSu" 指向的是一个关于LeetCode第76题的解决方案。这道题目要求找到一个给定字符串中最小子串,该子串包含了另一个给定的字符集(目标...

    python-leetcode题解之076-Minimum-Window-Substring

    python python_leetcode题解之076_Minimum_Window_Substring

    js-leetcode题解之76-minimum-window-substring.js

    javascript js_leetcode题解之76-minimum-window-substring.js

    c语言-leetcode题解之0076-minimum-window-substring.zip

    c c语言_leetcode题解之0076_minimum_window_substring.zip

    c++-c++编程基础之leetcode题解第76题最小覆盖子串.zip

    第76题"最小覆盖子串"(Minimum Window Substring)是其中一道经典的字符串处理问题,它涉及到字符串查找、滑动窗口等算法概念。在这个题解压缩包中,你将找到关于如何使用C++解决这道题目的详细步骤和代码实现。 ...

    1、基础算法必练题(含解法)).pdf

    12. **Minimum Window Substring**:找到包含所有目标字符的最小子串。滑动窗口或双指针技术是常用的解决方案。 13. **Merge Two Sorted Lists**:合并两个已排序的链表。可以使用迭代或递归方法,每次选取两个链表...

    dna匹配leetcode-leetcode:leetcode刷题

    Minimum Window Substring 两个指针遍历 map Maximal Rectangle 栈 局部递增 或者 动态规划 Binary Tree Inorder Traversal 栈 递归 Single Number 异或 Copy List with Random Pointer 单链表 map Max Points on a ...

    _leetcode-python.pdf

    - Minimum Window Substring: 给定两个字符串s和t,找出s中包含t所有字母的最小子串。 - Remove Duplicates from Sorted Array II: 删除排序数组中的重复项,使得每个元素最多出现两次。 - Search in Rotated Sorted...

    leetcode分类-leetcode:leetcode

    window 滑动窗口类型的题目经常是用来执行数组或是链表上某个区间(窗口)上的操作。比如找最长的全为1的子数组长度。滑动窗口一般从第一个元素开始,一直往右边一个一个元素挪动。 Leetcode Java Python 209. ...

    LeetCode最全代码

    411 | [Minimum Unique Word Abbreviation](https://leetcode.com/problems/minimum-unique-word-abbreviation/) | [C++](./C++/minimum-unique-word-abbreviation.cpp) [Python](./Python/minimum-unique-word-...

    leetcode添加元素使和等于-leetcode:我的leetcode解决方案

    Substring C++ STL中常见容器的时间复杂度 map, set, multimap, and multiset 上述四种容器采用红黑树实现,红黑树是平衡二叉树的一种。不同操作的时间复杂度近似为: 插入:O(logN) 查看:O(logN) 删除:O(logN) hash_...

    网页制作完全手册

    For example, if you have designed your content for the latest version of Internet Explorer, use that version number as a minimum version number. Note Browsers often have several releases of a ...

Global site tag (gtag.js) - Google Analytics