lintcode 中等题:minimum window substring 最小子串覆盖
题目
最小子串覆盖
给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串。
给出source = "ADOBECODEBANC",target = "ABC" 满足要求的解 "BANC"
如果在source中没有这样的子串,返回"",如果有多个这样的子串,返回起始位置最小的子串。
要求时间复杂度为O(n)
在答案的子串中的字母在目标字符串中是否需要具有相同的顺序?
——不需要。
解题
参考:
定义两个字典:tgt 、map
tgt统计target中每个字符出现的次数
map统计target和source出现的公共字符个数
再检测当tgt中所以的字符和map中的字符个数都匹配的时候就是一个子串了,再找出最小的子串就好了,程序中left值用来定义子串的右边界,要好好理解
public class Solution {
/**
* @param source: A string
* @param target: A string
* @return: A string denote the minimum window
* Return "" if there is no such a string
*/
public String minWindow(String source, String target) {
// write your code
int len1 = source.length();
int len2 = target.length();
if(len1 < len2)
return "";
String result = "";
// 统计target 中各个字符串出现的次数
HashMap<Character,Integer> tgt = new HashMap<Character,Integer>();
for(int i=0;i<len2;i++){
char c = target.charAt(i);
if(tgt.containsKey(c)){
tgt.put(c,tgt.get(c)+1);
}else{
tgt.put(c,1);
}
}
// 存放公共字符
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
int left = 0;
int minLen = len1+1;
int count =0;
for(int i=0;i<len1;i++){
char c = source.charAt(i);
if(tgt.containsKey(c)){
if(map.containsKey(c)){
if(map.get(c)<tgt.get(c)){
count++;
}
map.put(c,map.get(c)+1);
}else{
map.put(c,1);
count++;
}
}
// 说明是一个子串 去除left无效字符
if(count == len2){
char sc = source.charAt(left);
while(!map.containsKey(sc) || map.get(sc) > tgt.get(sc)){
if(map.containsKey(sc) && map.get(sc) > tgt.get(sc))
map.put(sc,map.get(sc) - 1);
left++;
sc = source.charAt(left);
}
// 找到最小子串
if( i - left + 1 < minLen){
result = source.substring(left,i + 1);
minLen = i - left + 1;
}
}
}
return result;
}
}
Java Code
lintcode 中等题:minimum window substring 最小子串覆盖的更多相关文章
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Minimum Window Substring, 包含子串的最小窗口,双指针
问题描述:给定字符串S,子串T,求S中包含T的最小窗口 Given a string S and a string T, find the minimum window in S which will ...
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
- [leetcode]76. Minimum Window Substring最小字符串窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [Leetcode] minimum window substring 最小字符窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
随机推荐
- Mongodb学习教程汇总
1.MongoDB权威指南 - 学习笔记 地址:http://www.cnblogs.com/refactor/category/394801.html 2.8天学通MongoDB 地址:http:/ ...
- php ftp文件上传函数--新手入门参考
在 php编程中,用ftp上传文件比较多见,这里分享个简单入门型的ftp上传实例. <?php /** * ftp上传文件 * 学习ftp函数的用法 */ // 定义变量 $local_file ...
- Spark 大数据平台
Apache Spark is an open source cluster computing system that aims to make data analytics fast - both ...
- 第一个leapmotion的小游戏
自从看过leapmotion的宣传视频,就被吸引住了.觉得这东西迟早要替代鼠标,然后关注了一年多leapmotion的动态,终于在今年8月份入手了一只.//675大洋啊,心疼~ 一直想写份评测,一直想 ...
- 记一次Surface Pro 2还原操作
因为要做Azure的一个case,对自己的域环境下直接进行了捕获.结果导致机器直接crash. 重启后使用本地账号登陆后发现所有Win 8 的App都无法使用,包括进入设置中还原方式也无法使用. 可以 ...
- 【转载】Powershell在世纪互联Office365中批量将用户添加到组
$NewUserPath = ".\Office365AddUserToGroup.csv" $NewUsers = import-csv $NewUserPath foreach ...
- linux c 实现大数相乘
#include <stdio.h> #include <string.h> #include <math.h> #include <stdbool.h& ...
- Sqlite: unable to open database file
A database connect, there updated both queries (different statement, and regardless of order), after ...
- CSS3 transition 属性 过渡效果
<!DOCTYPE html> <html> <head> <style> div { width:100px; height:100px; backg ...
- 阿里云mysql数据库恢复总结,mysql binlog日志解析
由于意外..阿里云mysql中有一张表被全部删除了,深吸三口气候,开始解决. 首先用凌晨的自动备份的,进行全量恢复,然后找binlog日志(见下文),查找从全量备份到数据删除之间的记录 这导致了一个问 ...