题目

最小子串覆盖

给定一个字符串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 最小子串覆盖的更多相关文章

  1. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  2. [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 ...

  3. Minimum Window Substring, 包含子串的最小窗口,双指针

    问题描述:给定字符串S,子串T,求S中包含T的最小窗口 Given a string S and a string T, find the minimum window in S which will ...

  4. 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...

  5. [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 ...

  6. [Leetcode] minimum window substring 最小字符窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  7. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  8. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  9. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

随机推荐

  1. MongoDB如何存储数据

    想要深入了解MongoDB如何存储数据之前,有一个概念必须清楚,那就是Memeory-Mapped Files. Memeory-Mapped Files 下图展示了数据库是如何跟底层系统打交道的. ...

  2. 在Linux中三种让crontab每秒执行任务的方法

    第一种方法: 1.创建脚本文件 cat phplog.sh 2.编辑脚本内容 #!/bin/bash while : ;do /home/scripts.sh 2>/dev/null & ...

  3. jQuery select的操作代码

    jQuery對select的操作的实际应用代码. //改變時的事件  复制代码代码如下: $("#testSelect").change(function(){ //事件發生  j ...

  4. Linux环境下Python的安装过程

    Linux环境下Python的安装过程 前言 一般情况下,Linux都会预装 Python了,但是这个预装的Python版本一般都非常低,很多 Python的新特性都没有,必须重新安装新一点的版本,从 ...

  5. 将Vim改造为强大的IDE

    1.安装Vim和Vim基本插件 首先安装好Vim和Vim的基本插件.这些使用apt-get安装即可: lingd@ubuntu:~/arm$sudo apt-get install vim vim-s ...

  6. c语言调试开关

    上一篇转载的没看懂,参考别人的代码,自己又琢磨了一个调试技巧,挺好用,姑且就叫调试开关吧,欢迎指正!!! /*功能:调试开关 *描述:if条件成立,则打印调试信息,否则不打印() * */ #incl ...

  7. python之类定义

    <python基础教程>第7章说python中的类定义: 1. 要么声明__metaclass__=type 2. 要么继承object. 但是直接定义下类, 也没报错: >> ...

  8. 编译linux内核问题

    1: openssl/opensslv.h: No such file or directory sudo apt-get install libssl-dev 2:一般配置内核树,需要先make o ...

  9. gcc编译出现的问题

    /usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error 解决办法:g++ -std=c++11

  10. 【python】 开始第一个项目

    根据这篇文章开始上手 http://www.oschina.net/translate/the-flask-mega-tutorial-part-i-hello-world 再加点东西 如果你的环境是 ...