当前位置:网站首页>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;
}
}
边栏推荐
- 全新探险者以40万的产品击穿豪华SUV价格壁垒
- Reptile attention
- ffplay视频播放原理分析
- No inner demons, to dry!SQL optimization and diagnosis
- 30W 2C(JD6606S + FP6652X2)BOM
- 如何选择合适的损失函数,请看......
- 【Unity入门计划】基本概念(7)-Input Manager&Input类
- 【Unity入门计划】基本概念(6)-精灵渲染器 Sprite Renderer
- 【Unity入门计划】制作RubyAdventure01-玩家的创建&移动
- 每日练习------有10个数字要求分别用选择法从大到小输出
猜你喜欢
随机推荐
MATLAB gcf图窗保存图像,黑色背景/透明背景
基于DMS的数仓智能运维服务,知多少?
美国国防部更“青睐”光量子系统研究路线
How Navicat connects to MySQL on a remote server
ECCV 2022 | Relational Query-Based Temporal Action Detection Methods
STM32的HAL和LL库区别和性能对比
How to play deep paging with hundreds of millions of data?Compatible with MySQL + ES + MongoDB
mysql delete 执行报错:You can‘t specify target table ‘doctor_info‘ for update in FROM clause
[Code Hoof Set Novice Village 600 Questions] Define a function as a macro
我在滴滴做开源
如何启动 NFT 集合
身为售后工程师的我还是觉得软件测试香,转行成功定薪11.5K,特来分享下经验。
深度学习——安装CUDA以及CUDNN实现tensorflow的GPU运行
一文看懂推荐系统:召回02:Swing 模型,和itemCF很相似,区别在于计算相似度的方法不一样
用户侧有什么办法可以自检hologres单表占用内存具体是元数据、计算、缓存的使用情况?
Awesome!Coroutines are finally here!Thread is about to be in the past
1、实例开启无锁表结构变更以后,在任务编排中通过“单实例SQL”节点进行的结构变更,是优先采用无锁表
leetcode: 899. Ordered Queue [Thinking Question]
自定SvgIcon公用组件
基于DMS的数仓智能运维服务,知多少?