【LeetCode】Minimum Window Substring
2014-04-28 17:56
453 查看
Minimum Window Substring
Total Accepted: 6795 Total Submissions: 39203 My Submissions
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.
【解题思路】
最小滑动窗口
按照C/C++描述来说,就是声明两个指针,不断滑动计算要求的最小长度。
具体而言,分为以下几步:
1、声明一个数组inArr,来统计源字符串T中每个字符出现的次数。
2、扫描字符串S,如果该字符存在T中,并且numArr[num1] <= inArr[num1],count++,这句话的意思就是目前所有的字符还不足以包含T中的所有字符。
3、判断count和T.length(),如果相等,就判断首指针是否还可以右移,到不能移动为止,就是一个最短长度。
4、重复2和3,就可以得到最小值。
Java AC
Total Accepted: 6795 Total Submissions: 39203 My Submissions
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.
【解题思路】
最小滑动窗口
按照C/C++描述来说,就是声明两个指针,不断滑动计算要求的最小长度。
具体而言,分为以下几步:
1、声明一个数组inArr,来统计源字符串T中每个字符出现的次数。
2、扫描字符串S,如果该字符存在T中,并且numArr[num1] <= inArr[num1],count++,这句话的意思就是目前所有的字符还不足以包含T中的所有字符。
3、判断count和T.length(),如果相等,就判断首指针是否还可以右移,到不能移动为止,就是一个最短长度。
4、重复2和3,就可以得到最小值。
Java AC
public class Solution { public String minWindow(String S, String T) { if (S == null || T == null || "".equals(S) || "".equals(T)) { return ""; } int size = 123; int inArr[] = new int[size]; int numArr[] = new int[size]; int len1 = S.length(); int len2 = T.length(); for (int i = 0; i < len2; i++) { inArr[T.charAt(i)]++; } int count = 0; int start = 0; int minLen = Integer.MAX_VALUE; String minS = ""; for (int end = 0; end < len1; end++) { int num1 = S.charAt(end); if (inArr[num1] != 0) { numArr[num1]++; if (numArr[num1] <= inArr[num1]) { count++; } if (count == len2) { while (inArr[S.charAt(start)] == 0 || numArr[S.charAt(start)] > inArr[S.charAt(start)]) { if (numArr[S.charAt(start)] > inArr[S.charAt(start)]) { numArr[S.charAt(start)]--; } start++; } int tempLen = end - start + 1; if (minLen > tempLen) { minLen = tempLen; minS = S.substring(start, end + 1); } } } } return minS; } }
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - Minimum Window Substring
- 【leetcode】Minimum Window Substring
- Leetcode -- Minimum Window Substring
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
- Leetcode 76 Minimum Window Substring(最小窗口的子串)
- leetcode: Minimum Window Substring
- leetcode: Minimum Window Substring
- LeetCode(76) Minimum Window Substring
- [leetcode刷题系列]Minimum Window Substring
- [leetcode] Minimum Window Substring
- Minimum Window Substring @LeetCode
- LeetCode: Minimum Window Substring
- [Leetcode] Minimum Window Substring
- minimum_window_substring ——leetcode
- 【leetcode】Minimum Window Substring (hard) ★
- LeetCode: Minimum Window Substring
- LeetCode(76)Minimum Window Substring
- [Leetcode][JAVA] Minimum Window Substring
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
- LeetCode_OJ【76】Minimum Window Substring