Minimum Window Substring Java
来源:互联网 发布:mac 查看下载速度 编辑:程序博客网 时间:2024/06/08 07:52
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.
Key to Solve: Two pointer Window's range method + HashMap,
Similar idea with Substring with Concatenation of All Words & Longest Substring Without Repeating Characters
1. Create a Map(key: Character, Value: Frequency) of T
2. move right window's point while < S.length
2. move left window's point under condition of full match were found
3. optimize the min window string
Time: O(2*N)=O(N)
Space: O(L) L is size of T
public class Solution { public String minWindow(String S, String T) { String output=""; if(S.length()==0 || T.length()==0) return ""; HashMap<Character,Integer> map=new HashMap<Character, Integer>(); int matchCount=0; int minLen=S.length()+1; int left=0,right=0; //create a Map for(int i=0;i<T.length();i++){ char c=T.charAt(i); if(map.containsKey(c)){ map.put(c,map.get(c)+1); }else { map.put(c,1); } } while(right<S.length()) { char c = S.charAt(right); if (map.containsKey(c)) { //System.out.println("c: "+c); map.put(c, map.get(c)-1); if (map.get(c) >= 0) { matchCount++; } //Full Match were found while (matchCount == T.length()) { char cl=S.charAt(left); if (map.containsKey(cl)) { //record the character frequency map.put(S.charAt(left), map.get(cl) + 1); //optimize the min window String if (map.get(cl)>0) { if (minLen > right - left + 1) { output = S.substring(left, right + 1); minLen = right - left + 1; } matchCount--; } } left++; } } right++; //skip un-relevant character } return output; }}
- Minimum Window Substring Java
- [Leetcode] Minimum Window Substring (Java)
- Minimum Window Substring LeetCode java
- [LeetCode][Java] Minimum Window Substring
- [leetcode-76]Minimum Window Substring(java)
- leetcode:Minimum Window Substring题解(Java)
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- Minimum Window Substring
- LeetCode: Minimum Window Substring
- Minimum Window Substring
- [LeetCode]Minimum Window Substring
- Minimum Window Substring
- Minimum Window Substring
- [leetcode]Minimum Window Substring
- map容器使用基础
- 表空间的状态(二) - read/write
- [HDOJ 4949] Light [插头DP]
- ScrollView与ListView合用(TextView多行显示时计算不正确)的问题解决
- UIScrollView介绍
- Minimum Window Substring Java
- soapui模拟桩mockservice---发布mockservice
- Android Fragment
- mysql中的case when then,replace的用法。
- 8-18 Android学习ing
- 记ACM第一次大聚餐!
- 一个Action多个方法的调用
- Flex实践——事件监听与处理
- PreparedStatement的用法