[LeetCode] Minimum Window Substring
来源:互联网 发布:移动互联网数据报告 编辑:程序博客网 时间:2024/04/30 09:32
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"
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
和很多字符串包含的题思路一样,都是维护S字符串的一个窗口,窗口右边一直往后移动,直到窗口内包含所有T中字符串停止,此时窗口左边开始移动,直到窗口刚好不满足包含T中所有字符串则停止,计算更新最小串。
public class Solution { public String minWindow(String S, String T) { if (S == null || T == null) { return ""; } Map<Character, Integer> map = new HashMap<Character, Integer>(); for (int i = 0; i < T.length(); i++) { char curChar = T.charAt(i); if (map.containsKey(curChar)) { map.put(curChar, map.get(curChar) + 1); } else { map.put(curChar, 1); } } String res = ""; int minLen = S.length() + 1; int pre = 0; int count = 0; int len = T.length(); for (int i = 0; i < S.length(); i++) { char curChar = S.charAt(i); if (map.containsKey(curChar)) { map.put(curChar, map.get(curChar) - 1); if (map.get(curChar) >= 0) { count++; } while (count == len) { if (map.containsKey(S.charAt(pre))) { map.put(S.charAt(pre), map.get(S.charAt(pre)) + 1); if (map.get(S.charAt(pre)) > 0) { count--; } if (i - pre + 1 < minLen) { res = S.substring(pre, i + 1); minLen = i - pre + 1; } } pre++; } } } return res; }}
0 0
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- LeetCode: Minimum Window Substring
- [LeetCode]Minimum Window Substring
- [leetcode]Minimum Window Substring
- Leetcode:Minimum Window Substring
- LeetCode-Minimum Window Substring
- [leetcode] Minimum Window Substring
- [leetcode] Minimum Window Substring
- leetcode Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [LeetCode] Minimum Window Substring
- leetcode minimum window substring
- LeetCode - Minimum Window Substring
- LeetCode:Minimum Window Substring
- Java中equals和==的区别
- 通过二分的方法查找小于n的素数的个数
- 好东西,大家一起分享呀,送福利了,喜欢编程的小伙伴
- 网站长尾词优化排名,你知道怎么做最有效吗?
- CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2
- [LeetCode] Minimum Window Substring
- Maximum repetition substring+POj+后缀数组之求重复次数最多的连续重复子串
- Oracle 左外连接的一些测试
- Ejabberd作为推送服务的优化手段
- OpenCV基础篇之读取显示图片
- css背景图片拉伸填充避免重复显示
- ZoneGridVIew 自定义放大GridView
- Neutron L3 auto Reschedule VRouter feature
- 一些优秀的移动开发网址