博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Minimum Window Substring
阅读量:4074 次
发布时间:2019-05-25

本文共 1690 字,大约阅读时间需要 5 分钟。

Minimum Window Substring

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.

Java代码:

 
public class Solution {public String minWindow(String S, String T) {    int[] result = new int[] {-1, S.length()};    int counter = 0;    Map
expected = new HashMap<>(); Map
window = new HashMap<>(); for (int i = 0; i < T.length(); i++) { if (!expected.containsKey(T.charAt(i))) expected.put(T.charAt(i), 0); expected.put(T.charAt(i), expected.get(T.charAt(i)) + 1); } for (int i = 0, j = 0; j < S.length(); j++) { char cur = S.charAt(j); if (expected.containsKey(cur)) { if (!window.containsKey(cur)) window.put(cur, 0); window.put(cur, window.get(cur) + 1); if (window.get(cur) <= expected.get(cur)) counter++; if (counter == T.length()) { char remove = S.charAt(i); while (!expected.containsKey(remove) || window.get(remove) > expected.get(remove)){ if (expected.containsKey(remove)) window.put(remove, window.get(remove) - 1); remove = S.charAt(++i);; } if (j - i < result[1] - result[0]) result = new int[]{i, j}; } } } return result[1] - result[0] < S.length() ? S.substring(result[0], result[1] + 1) : "";}}

转载地址:http://mvuni.baihongyu.com/

你可能感兴趣的文章
Yotta企业云盘助力科技行业创高峰
查看>>
Yotta企业云盘更好地为教育行业服务
查看>>
Yotta企业云盘怎么帮助到能源化工行业
查看>>
企业云盘如何助力商业新发展
查看>>
医疗行业运用企业云盘可以带来什么样的提升
查看>>
媒体广告业如何运用云盘提升效率
查看>>
IOS开发的开源库
查看>>
Jenkins - sonarqube 代码审查
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成(一)
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成 - 单机部署(二)
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成 - 高可用集群部署(三)
查看>>
Linux 粘滞位 suid sgid
查看>>
C#控件集DotNetBar安装及破解
查看>>
Winform皮肤控件IrisSkin4.dll使用
查看>>
Winform多线程
查看>>
C# 托管与非托管
查看>>
Node.js中的事件驱动编程详解
查看>>
mongodb管理与安全认证
查看>>
nodejs内存控制
查看>>
nodejs Stream使用中的陷阱
查看>>