转载自leetcode评论区:https://discuss.leetcode.com/topic/30941/here-is-a-10-line-template-that-can-solve-most-substring-problems

I will first give the solution then show you the magic template.

The code of solving this problem is below. It might be the shortest among all solutions provided in Discuss.

string minWindow(string s, string t) {
vector<int> map(128,0);
for(auto c: t) map[c]++;
int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0;
while(end<s.size()){
if(map[s[end++]]-->0) counter--; //in t
while(counter==0){ //valid
if(end-begin<d) d=end-(head=begin);
if(map[s[begin++]]++==0) counter++; //make it invalid
}
}
return d==INT_MAX? "":s.substr(head, d);
}

Here comes the template.

For most substring problem, we are given a string and need to find a substring of it which satisfy some restrictions. A general way is to use a hashmap assisted with two pointers. The template is given below.

int findSubstring(string s){
vector<int> map(128,0);
int counter; // check whether the substring is valid
int begin=0, end=0; //two pointers, one point to tail and one head
int d; //the length of substring for() { /* initialize the hash map here */ } while(end<s.size()){ if(map[s[end++]]-- ?){ /* modify counter here */ } while(/* counter condition */){ /* update d here if finding minimum*/ //increase begin to make it invalid/valid again if(map[s[begin++]]++ ?){ /*modify counter here*/ }
} /* update d here if finding maximum*/
}
return d;
}

One thing needs to be mentioned is that when asked to find maximum substring, we should update maximum after the inner while loop to guarantee that the substring is valid. On the other hand, when asked to find minimum substring, we should update minimum inside the inner while loop.

The code of solving Longest Substring with At Most Two Distinct Characters is below:

int lengthOfLongestSubstringTwoDistinct(string s) {
vector<int> map(128, 0);
int counter=0, begin=0, end=0, d=0;
while(end<s.size()){
if(map[s[end++]]++==0) counter++;
while(counter>2) if(map[s[begin++]]--==1) counter--;
d=max(d, end-begin);
}
return d;
}

The code of solving Longest Substring Without Repeating Characters is below:

Update 01.04.2016, thanks @weiyi3 for advise.

int lengthOfLongestSubstring(string s) {
vector<int> map(128,0);
int counter=0, begin=0, end=0, d=0;
while(end<s.size()){
if(map[s[end++]]++>0) counter++;
while(counter>0) if(map[s[begin++]]-->1) counter--;
d=max(d, end-begin); //while valid, update d
}
return d;
}

I think this post deserves some upvotes! : )

Here is a 10-line template that can solve most 'substring' problems子字符串问题的模板的更多相关文章

  1. Spring框架学习10——JDBC Template 实现数据库操作

    为了简化持久化操作,Spring在JDBC API之上提供了JDBC Template组件. 1.添加依赖 添加Spring核心依赖,MySQL驱动 <!--Spring核心基础依赖--> ...

  2. WPF 10天修炼 第七天- WPF资源、样式、控件模板

    WPF资源 对象资源 WPF允许在XAML标记的任意位置定义资源.比如在特定的控件.窗口或应用程序级别定义资源,WPF资源系统提供的对象资源有如下好处: 1.  高效:使用对象资源可以在一个地方定义而 ...

  3. idea Error:(1, 10) java: 需要class, interface或enum, 未结束的字符串文字,Error:(55, 136) java: 非法字符: \65533

    1.未结束的字符串文字,Error:(55, 136) java: 非法字符: \65533  这些乱七吧八遭的错误如果很多的话 , 尝试 重新修改下生成目录 修改下语言等级 上述方法都不行 ,还报错 ...

  4. T4((Text Template Transformation Toolkit))模版引擎之基础入门 C#中文本模板(.tt)的应用

    1 关于C#中文本模板(.tt)的简单应用https://blog.csdn.net/zunguitiancheng/article/details/78011145 任何一个傻瓜都能写出计算机能理解 ...

  5. 速查笔记(Linux Shell编程<上>)

    转载自: http://www.cnblogs.com/stephen-liu74/archive/2011/11/01/2202027.html 零.shell中的内部变量: 1.    $?:   ...

  6. linux命令帮助 man bash

    BASH(1) BASH(1) NAME bash - GNU Bourne-Again SHell (GNU 命令解释程序 “Bourne二世”) 概述(SYNOPSIS) bash [option ...

  7. R(五): R常用函数

    工作笔记记录,会持续更新.... 目录: apply tapply lapply sapply merge substr.substring.strsplit.unlist.paste.paste0. ...

  8. [置顶] 一个demo学会c#

    学习了c#4.5高级编程这本书,自己喜欢边学边总结边写demo,所以写了这篇文章,包含了大部分的c#编程知识.让你一个demo掌握c#编程,如果有问题可以留言. 此demo主要包括五个文件:Stude ...

  9. Mysql运算符与函数(胖胖老师)

    use test;create table `employee`(    emp_no int unsigned,    emp_name varchar(30),    emp_sex varcha ...

随机推荐

  1. 【leetcode 简单】 第六十四题 翻转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 ...

  2. seq与Shell序列生成

    有时候可能有这样的需要:用Shell生成类似0001这样的序列作为批次号,这里整理了一下个人的方法 方法一:通过seq命令 seq命令可以生成从某个数字到递增到另一数字的序列.用法如下: # seq ...

  3. 状压dp+floyed(C - Hie with the Pie POJ - 3311 )

    题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...

  4. Linux USB驱动框架分析(2)【转】

    转自:http://blog.chinaunix.net/uid-23046336-id-3243543.html   看了http://blog.chinaunix.net/uid-11848011 ...

  5. ORACLE表空间查询和管理【转】

    红色是自由指定的~~--查询表空间SELECT D.TABLESPACE_NAME,       SPACE "SUM_SPACE(M)",       SPACE - NVL(F ...

  6. gnuplot生成MySQL QPS图形

    1.建立MySQL QPS执行脚本 #!/bin/bash mysqladmin -uroot -p' extended-status -i1|awk \ 'BEGIN{flag=0; print & ...

  7. 27 Debugging Go Code with GDB 使用GDB调试go代码

    Debugging Go Code with GDB  使用GDB调试go代码 Introduction Common Operations Go Extensions Known Issues Tu ...

  8. http://code52.org/DownmarkerWPF/

    http://code52.org/DownmarkerWPF/ http://kb.cnblogs.com/page/132209/

  9. python中的多进程

    具体参考这个博客地址:http://www.cnblogs.com/lxmhhy/p/6052167.html

  10. 制作macOS10.12系列的系统镜像文件

    制作macOS10.12系列的系统镜像文件步骤,过程也比较简单,十来个命令.以10.12.6为例,首先,在苹果商店下载系统安装包APP,或者网上下载后把安装APP复制到  应用程序  文件夹. 然后打 ...