leetcode: 76. Minimum Window Substring
来源:互联网 发布:java考试认证 编辑:程序博客网 时间:2024/06/05 15:44
Q
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 empty string “”.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
AC
# Time: O(n)# Space: O(k), k is the number of different charactersclass Solution(object): def minWindow(self, s, t): """ :type s: str :type t: str :rtype: str """ current_count = [0 for i in xrange(52)] expected_count = [0 for i in xrange(52)] for char in t: expected_count[ord(char) - ord('a')] += 1 i, count, start, min_width, min_start = 0, 0, 0, float("inf"), 0 while i < len(s): current_count[ord(s[i]) - ord('a')] += 1 if current_count[ord(s[i]) - ord('a')] <= expected_count[ord(s[i]) - ord('a')]: count += 1 if count == len(t): while expected_count[ord(s[start]) - ord('a')] == 0 or \ current_count[ord(s[start]) - ord('a')] > expected_count[ord(s[start]) - ord('a')]: current_count[ord(s[start]) - ord('a')] -= 1 start += 1 if min_width > i - start + 1: min_width = i - start + 1 min_start = start i += 1 if min_width == float("inf"): return "" return s[min_start:min_start + min_width]if __name__ == "__main__": assert Solution().minWindow("ADOBECODEBANC", "ABC") == 'BANC'
阅读全文
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
- iOS开发-RuntimeDebug运行时源码调试(macOS APP)
- C#-动态编程
- .NET Entity Framework(EF)使用SqlQuery直接操作SQL查询语句或者执行过程
- C#获取本机MAC地址和IP
- (基于UDP协议/tcp协议)socket客户端,服务端
- leetcode: 76. Minimum Window Substring
- Netty学习(二)—拆包粘包问题
- 图说2017年C++大会
- (转)No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- LSV云端上传下载矢量数据
- Ubuntu终端常用的快捷键
- leetcode: 77. Combinations
- kNN(K-Nearest Neighbor)算法简介
- tail命令及时查看文件变化