Leetcode 76. Minimum Window Substring
来源:互联网 发布:java txt换行符 编辑:程序博客网 时间:2024/06/03 13:13
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"
.
public String minWindow(String s, String t) { if (s == null || s.length() == 0) return ""; Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < t.length(); i++) { char c = t.charAt(i); map.put(c, map.getOrDefault(c, 0) + 1); } int l = 0; int cnt = 0; int start = 0; int minLength = s.length() + 1; for (int r = 0; r < s.length(); r++) { if (map.containsKey(s.charAt(r))) { map.put(s.charAt(r), map.get(s.charAt(r)) - 1); if (map.get(s.charAt(r)) >= 0) cnt++; while (cnt == t.length()) { if (r - l + 1 < minLength) { minLength = r - l + 1; start = l; } if (map.containsKey(s.charAt(l))) { map.put(s.charAt(l), map.get(s.charAt(l)) + 1); if (map.get(s.charAt(l)) > 0) cnt--; } l++; } } } return minLength > s.length() ? "" : s.substring(start, start + minLength); }
阅读全文
0 0
- LeetCode 76. Minimum Window Substring
- [LeetCode]76.Minimum Window Substring
- LeetCode 76.Minimum Window Substring
- LeetCode --- 76. Minimum Window Substring
- [Leetcode] 76. Minimum Window Substring
- [leetcode] 76.Minimum Window Substring
- **LeetCode 76. Minimum Window Substring
- leetcode 76. Minimum Window Substring
- 【LeetCode】76. Minimum Window Substring
- LeetCode 76. Minimum Window Substring
- Leetcode 76. Minimum Window Substring
- leetcode.76. Minimum Window Substring
- LeetCode 76. Minimum Window Substring
- [LeetCode] 76. Minimum Window Substring
- leetcode 76.Minimum Window Substring
- leetcode 76. Minimum Window Substring
- 76. Minimum Window Substring, leetcode
- leetcode-76. Minimum Window Substring
- Python网络爬虫——Beautiful Soup库
- hive的安装
- Mybatis学习笔记(二)-Mybatis配置文件与映射文件详解
- 一些实用的开源炫酷框架—值得你去了解
- VS2013 密钥
- Leetcode 76. Minimum Window Substring
- 小白浅谈Linux
- MySQL外连接
- MapReduce 类的封装思想
- leetcode中字符串转换为整数
- JQUERY实现列表检索功能
- Retrofit 2.0 使用总结
- MySQL复合条件连接查询
- java反射