Here is a 10-line template that can solve most 'substring' problems子字符串问题的模板
转载自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子字符串问题的模板的更多相关文章
- Spring框架学习10——JDBC Template 实现数据库操作
为了简化持久化操作,Spring在JDBC API之上提供了JDBC Template组件. 1.添加依赖 添加Spring核心依赖,MySQL驱动 <!--Spring核心基础依赖--> ...
- WPF 10天修炼 第七天- WPF资源、样式、控件模板
WPF资源 对象资源 WPF允许在XAML标记的任意位置定义资源.比如在特定的控件.窗口或应用程序级别定义资源,WPF资源系统提供的对象资源有如下好处: 1. 高效:使用对象资源可以在一个地方定义而 ...
- idea Error:(1, 10) java: 需要class, interface或enum, 未结束的字符串文字,Error:(55, 136) java: 非法字符: \65533
1.未结束的字符串文字,Error:(55, 136) java: 非法字符: \65533 这些乱七吧八遭的错误如果很多的话 , 尝试 重新修改下生成目录 修改下语言等级 上述方法都不行 ,还报错 ...
- T4((Text Template Transformation Toolkit))模版引擎之基础入门 C#中文本模板(.tt)的应用
1 关于C#中文本模板(.tt)的简单应用https://blog.csdn.net/zunguitiancheng/article/details/78011145 任何一个傻瓜都能写出计算机能理解 ...
- 速查笔记(Linux Shell编程<上>)
转载自: http://www.cnblogs.com/stephen-liu74/archive/2011/11/01/2202027.html 零.shell中的内部变量: 1. $?: ...
- linux命令帮助 man bash
BASH(1) BASH(1) NAME bash - GNU Bourne-Again SHell (GNU 命令解释程序 “Bourne二世”) 概述(SYNOPSIS) bash [option ...
- R(五): R常用函数
工作笔记记录,会持续更新.... 目录: apply tapply lapply sapply merge substr.substring.strsplit.unlist.paste.paste0. ...
- [置顶]
一个demo学会c#
学习了c#4.5高级编程这本书,自己喜欢边学边总结边写demo,所以写了这篇文章,包含了大部分的c#编程知识.让你一个demo掌握c#编程,如果有问题可以留言. 此demo主要包括五个文件:Stude ...
- Mysql运算符与函数(胖胖老师)
use test;create table `employee`( emp_no int unsigned, emp_name varchar(30), emp_sex varcha ...
随机推荐
- 基本控件文档-UISwitch属性
CHENYILONG Blog 基本控件文档-UISwitch属性 Fullscreen UISwitch属性 技术博客http://www.cnblogs.com/ChenYilong/ 新 ...
- 差分约束系统 + spfa(A - Layout POJ - 3169)
题目链接:https://cn.vjudge.net/contest/276233#problem/A 差分约束系统,假设当前有三个不等式 x- y <=t1 y-z<=t2 x-z< ...
- 【codeforces】940F题解
CF Round #466的最后一题,颇有难度,正解是带修改莫队算法. [题意] 给定一个长度为\(n\)的数组\(a\),并且要求执行\(q\)个操作,有两种不同的操作: ①询问一个区间\([l,r ...
- 戴尔游匣5577安装 ubuntu/mint
这里以mint为例. 做好usb启动盘后, 启动到读秒的时候按上下方向键唤出如下选项: 在第二个选项里按 tab 键(上图界面有提示) 进去后有如下界面: 在后面加上参数: nouveau.modes ...
- Maven仓库国内镜像站
感谢阿里巴巴,搭建并公开了Maven仓库的国内镜像站.话外:使用Maven的官方仓库真的是太slow了! 在<Maven Root>/conf/settings.xml中的<mirr ...
- 跳出python的各种坑(1)
2017-11-1915:38:17 一定要跳出python的各种坑,一开始遇到的好多思维上的认知错误,因为刚开始学习,对python是个什么都不清楚,所以记录一下自己遇到的各种坑.不用担心自己遇到的 ...
- APUE-文件和目录(八)文件时间
文件的时间 与文件相关的三个时间值: 访问时间:最后一次访问文件的时间.例如,cat命令会修改这个时间. 修改时间:文件内容最后一次被修改的时间. 状态更改时间:文件的i节点最后一次被修改的时间.例如 ...
- 使用JS实现2048小游戏
JS实现2048小游戏源码 效果图: 代码如下,复制即可使用: (适用浏览器:360.FireFox.Chrome.Opera.傲游.搜狗.世界之窗. 不支持Safari.IE8及以下浏览器.) &l ...
- 配置vuejs加载模拟数据
[个人笔记,非技术博客] 1.使用前确保安装axios插件,vuejs官方推荐,当然使用其他插件也可以 2.配置dev-server.js var router = express.Router(); ...
- 算法笔试题整理——升级蓄水池 && 字符串数字表达式计算值 && 求旅游完所有景点需要的最少天数 && 宝箱怪
1. 小米笔试题——升级蓄水池 题目描述: 在米兔生活的二维世界中,建造蓄水池非常简单. 一个蓄水池可以用n个坐标轴上的非负整数表示,代表区间为[0-n]范围内宽度为1的墙壁的高度. 如下图1,黑色部 ...