当前位置:网站首页>leetcode/含有所有字符的最短字符串
leetcode/含有所有字符的最短字符串
2022-08-04 17:43:00 【xcrj】
代码
package com.xcrj;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/** * 剑指 Offer II 017. 含有所有字符的最短字符串 * t中的所有字符都要包含,t中若有重复的字符也要包含 * 给定两个字符串 s 和t 。返回 s 中包含t的所有字符的最短子字符串。如果 s 中不存在符合条件的子字符串,则返回空字符串 "" 。 * 如果 s 中存在多个符合条件的子字符串,返回任意一个。 * 注意 s和t中含有大写或小写字母,因此不能简单直接使用solution15变位词的解法 */
public class Solution17 {
// Map<字符,出现次数>
private Map<Character, Integer> tmap = new HashMap<>();
//
private Map<Character, Integer> smap = new HashMap<>();
/** * 双指针 * r不停的右移,直到l~r的字符都包含在t中 * t的所有字符仍然被包含,l左移 */
public String minWindow1(String s, String t) {
// 特殊情况
if (s.length() < t.length()) {
return "";
}
// 统计t中字符
for (char c : t.toCharArray()) {
tmap.put(c, tmap.getOrDefault(c, 0) + 1);
}
int minLen = Integer.MAX_VALUE;
int start = 0;
int l = 0;
int r = -1;
// !!!记住这个滑动窗口的双wihle模板代码
while (r < s.length()) {
// 右移r:r不停的右移,直到l~r的字符都包含在t中
r++;
if (r < s.length() && tmap.containsKey(s.charAt(r))) {
// 统计t包含 s字符串的l~r字符
smap.put(s.charAt(r), smap.getOrDefault(s.charAt(r), 0) + 1);
}
// 右移l:检查l~r的s子串中的每个字符的数量>=t中每个字符的数量,尝试右移l
while (l <= r && check1()) {
// 右移l过程中判断最小长度
if (r - l + 1 < minLen) {
minLen = r - l + 1;
start = l;
}
// 统计,剔除l指向的s中的字符
if (smap.containsKey(s.charAt(l))) {
smap.put(s.charAt(l), smap.get(s.charAt(l)) - 1);
}
l++;
}
}
return minLen == Integer.MAX_VALUE ? "" : s.substring(start, start + minLen);
}
/** * l~r的s子串中的每个字符的数量>=t中每个字符的数量 * 检查t字符串中的所有字符是否都被包含在了 s字符串中的l~r中 * 已知 t字符串的统计散列表 tmap * 已知 s字符串l~r的统计散列表 smap */
public boolean check1() {
for (Map.Entry<Character, Integer> tentry : tmap.entrySet()) {
// l~r在smap中统计的字符数量 一定要>= tmap统计的字符数量,如果小于则不满足 s(l~r)要包含t中所有字符的条件,如果>则存在右移的机会
if (smap.getOrDefault(tentry.getKey(), 0) < tentry.getValue()) {
return false;
}
}
// l~r的s子串中的每个字符的数量>=t中每个字符的数量
return true;
}
public static void main(String[] args) {
Solution17 solution17 = new Solution17();
System.out.println(solution17.minWindow1("a", "b"));
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/M1oyTv/solution/han-you-suo-you-zi-fu-de-zui-duan-zi-fu-a8q50/
来源:力扣(LeetCode)
边栏推荐
- 身为程序员的我们如何卷死别人?破局重生。
- 如何让 JS 代码不可断点
- CAS:474922-26-4,DSPE-PEG-NH2,DSPE-PEG-amine,磷脂-聚乙二醇-氨基供应
- LeetCode 每日一题——1403. 非递增顺序的最小子序列
- DMPE-PEG-Mal,二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺简述
- 下一代 AutoAI:从模型为中心,到数据为中心
- JWT主动校验Token是否过期
- Thrift IDL Sample File
- Qt自动补全之QCompleter使用
- OpenInfra Days China 2022|SelectDB与你共享 Apache Doris 在互联网广告业务中的实践
猜你喜欢

OpenInfra Days China 2022 | SelectDB to share with you the Apache Doris in Internet advertising business practices
![【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model](/img/db/44a1ac5338879c9e6edd933c28c0af.png)
【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model

LeetCode 每日一题——1403. 非递增顺序的最小子序列

SQL优化最全总结 - MySQL(2022最新版)

【web自动化测试】Playwright快速入门,5分钟上手
软件测试高频面试题真实分享/网上银行转账是怎么测的,设计一下测试用例。

租房小程序登顶码云热门

荣耀互联对外开放,赋能智能硬件合作伙伴,促进全场景生态产品融合

Introduction of three temperature measurement methods for PT100 platinum thermal resistance

Learning to Explore - Setting the Foreground Color for Fonts
随机推荐
shell函数内如何调用另一个函数
R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,nrow参数指定行的个数、byrow参数指定按照列顺序排布图
集群监控——Zabbix使用
88.(cesium之家)cesium聚合图
微信jsApi调用失效的相关问题
华为云计算HCIE之oceanstor仿真器的安装教程
codeforces每日5题(均1600)-第二十八天
下一代 AutoAI:从模型为中心,到数据为中心
树莓派通过API向企业微信推送图文
数字化金融企业的产品体系长啥样?
太一集团全资收购火币旗下社交产品火信
《机器学习的随机矩阵方法》
Create Sentinel high-availability cluster current limiting middleware from -99
《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
SRM Supplier Collaborative Management System Function Introduction
最小区间覆盖
从-99打造Sentinel高可用集群限流中间件
身为程序员的我们如何卷死别人?破局重生。
R语言缺失时间序列的填充及合并:补齐时间序列数据中所有缺失的时间索引、使用merge函数合并日期补齐之后的时间序列数据和另外一个时间序列数据(补齐左侧数据)
网页端IM即时通讯开发:短轮询、长轮询、SSE、WebSocket