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; } }
相关推荐
标题 "LeetCode76-LeetCode76_MinimumWindowSubstring:LeetCode76_MinimumWindowSu" 指向的是一个关于LeetCode第76题的解决方案。这道题目要求找到一个给定字符串中最小子串,该子串包含了另一个给定的字符集(目标...
python python_leetcode题解之076_Minimum_Window_Substring
javascript js_leetcode题解之76-minimum-window-substring.js
c c语言_leetcode题解之0076_minimum_window_substring.zip
第76题"最小覆盖子串"(Minimum Window Substring)是其中一道经典的字符串处理问题,它涉及到字符串查找、滑动窗口等算法概念。在这个题解压缩包中,你将找到关于如何使用C++解决这道题目的详细步骤和代码实现。 ...
12. **Minimum Window Substring**:找到包含所有目标字符的最小子串。滑动窗口或双指针技术是常用的解决方案。 13. **Merge Two Sorted Lists**:合并两个已排序的链表。可以使用迭代或递归方法,每次选取两个链表...
Minimum Window Substring 两个指针遍历 map Maximal Rectangle 栈 局部递增 或者 动态规划 Binary Tree Inorder Traversal 栈 递归 Single Number 异或 Copy List with Random Pointer 单链表 map Max Points on a ...
- Minimum Window Substring: 给定两个字符串s和t,找出s中包含t所有字母的最小子串。 - Remove Duplicates from Sorted Array II: 删除排序数组中的重复项,使得每个元素最多出现两次。 - Search in Rotated Sorted...
window 滑动窗口类型的题目经常是用来执行数组或是链表上某个区间(窗口)上的操作。比如找最长的全为1的子数组长度。滑动窗口一般从第一个元素开始,一直往右边一个一个元素挪动。 Leetcode Java Python 209. ...
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-...
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 ...