当前位置:网站首页>Leetcode76. Minimal Covering Substring
Leetcode76. Minimal Covering Substring
2022-08-03 16:04:00 【Java Full Stack R&D Alliance】
题目传送地址: https://leetcode.cn/problems/minimum-window-substring/
运行效率
代码如下:
class Solution {
//滑动窗口解法 In order to enhance the contrast of two strings efficiency, 于是用了map,Dynamic maintenance of sliding windowmap
public static String minWindow(String s, String t) {
//处理边界情况
if ("".equals(s)) {
return "";
}
if (s.length() == 1) {
if (s.equals(t)) {
return s;
}
return "";
}
String result = "";
//Maintain a dynamic sliding windowMap,Identifies the window in the number of occurrences of each characters
Map<Character, Integer> tMap = getTmap(t);
Map<Character, Integer> sMap = new HashMap<>();
//滑动窗口
int left = 0; //滑动窗口的左指针
int right = 0; //Sliding window right pointer
sMap.put(s.charAt(0), 1);
while (right < s.length()) {
if (right - left + 1 < t.length()) {
right++;
if (right < s.length()) {
putValToMap(s.charAt(right), sMap);
}
continue;
}
Character character = vaildStr(sMap, tMap);
if (character == null) {
//如果验证通过
String substring = s.substring(left, right + 1);
if ("".equals(result)) {
result = substring;
} else {
if (substring.length() < result.length()) {
result = substring;
}
}
deleteValToMap(s.charAt(left), sMap); //Start with the sliding windowmap里删除,And then the left pointer moves to the right,Don't make the order
left++;
} else {
right++;
if (right < s.length()) {
putValToMap(s.charAt(right), sMap);
}
while (right < s.length()) {
char c = s.charAt(right);
if (c == character) {
break;
}
right++;
if (right < s.length()) {
putValToMap(s.charAt(right), sMap);
}
}
}
if (result.length() == t.length()) {
return result;
}
}
return result;
}
public static void putValToMap(Character c, Map<Character, Integer> map) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
public static void deleteValToMap(Character c, Map<Character, Integer> map) {
if (map.containsKey(c)) {
Integer count = map.get(c);
if (count == 1) {
map.remove(c);
} else {
map.put(c, count - 1);
}
}
}
public static Map<Character, Integer> getTmap(String t) {
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
char c = t.charAt(i);
if (map.containsKey(c)) {
Integer count = map.get(c);
map.put(c, count + 1);
} else {
map.put(c, 1);
}
}
return map;
}
/** * Validate elements within a sliding window, whether they contain all the elements of the target object * @param sMap * @param tMap * @return Returns the missing elements */
public static Character vaildStr(Map<Character, Integer> sMap, Map<Character, Integer> tMap) {
Iterator<Character> iterator = tMap.keySet().iterator();
while (iterator.hasNext()) {
Character c = iterator.next();
Integer tCount = tMap.get(c);
if (sMap.containsKey(c)) {
Integer sCount = sMap.get(c);
if (sCount < tCount) {
return c;
}
} else {
return c;
}
}
return null;
}
}
边栏推荐
- 小熊派——无线联网开发
- AWS中国区SDN Connector
- Yii2安装遇到Loading composer repositories with package information
- posgresql 到 es 报这个错误 ,啥意思
- 开源一夏 | 打工人的第25天-曾经的考研人
- 一个文件管理系统的软硬件配置清单
- 微电网和直流电网中最优潮流(OPF)的凸优化(Matlab代码实现)
- JS handwritten call apply bind (detailed) (interview)
- 1、实例开启无锁表结构变更以后,在任务编排中通过“单实例SQL”节点进行的结构变更,是优先采用无锁表
- 【899. Ordered Queue】
猜你喜欢
如何启动 NFT 集合
NodeJs - cross domain
扫雷?拿来吧你(递归展开+坐标标记)
瞌睡检测系统介绍
Research on power flow in DC microgrid based on Newton's method (Matlab code implementation)
AI也有健忘症?英国41岁教授专访:解决灾难性遗忘
Optimal Power Flow (OPF) for High Voltage Direct Current (HVDC) (Matlab code implementation)
劲爆!协程终于来了!线程即将是过去式
Awesome!Coroutines are finally here!Thread is about to be in the past
全新探险者以40万的产品击穿豪华SUV价格壁垒
随机推荐
MySQL中的基数是啥?
无内鬼,来点干货!SQL优化和诊断
袁小林:沃尔沃专注于出行的安全感,并且把它做到极致
深入浅出Flask PIN
一个文件管理系统的软硬件配置清单
如何将二维空间先验注入到ViT中? UMA&港理工&阿里提出SP-ViT,为视觉Transformer学习2D空间先验知识!...
并发编程的核心问题
GTK实现旋转加载动画
【QT】Qt 给已经开发好的程序快速封装成动态库
【数据库数据恢复】SqlServer数据库无法读取的数据恢复案例
【899. Ordered Queue】
【深度学习】今日bug(8月2)
mysql delete execution error: You can't specify target table 'doctor_info' for update in FROM clause
不可忽略!户外LED显示屏的特点及优势
扩展欧几里得求逆元实例
方舟开服工具、服务器教程win
DC-DC 2C(40W/30W) JD6606SX2退功率应用
我在滴滴做开源
分享一款免费OPC UA服务器
深度学习——安装CUDA以及CUDNN实现tensorflow的GPU运行