【LeetCode】 子字符串思路
在一些求字串含有固定字符最短串,含有不同字符最长子串等问题中,利用 vector<int> map(128, 0)可以解决
题一:最短给定子串
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
- If there is no such window in S that covers all characters in T, return the empty string
"". - If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
思路:
利用map,将常用的128个ascall码都包含进去,然后将T里有的字符在map进行累加,并计数,然后在S里找子串,完全找到后计数为零,然后继续找,直到找到最短的子串。
class Solution {
public:
string minWindow(string s, string t) {
vector<int> map(,);
int counter=t.size(), begin=, end=, min_length=INT_MAX, head = ;
for(auto a : t) map[a]++;
while(end<s.size()){
if(map[s[end++]]-- > ) counter--;
while(counter == ){
if(min_length > end - begin) min_length = end - (head = begin);
if(map[s[begin++]]++ == ) counter++;
}
}
return min_length == INT_MAX ? "" : s.substr(head, min_length);
}
};
模板
int findSubstring(string s){
vector<int> map(,);
int counter; // check whether the substring is valid
int begin=, end=; //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;
}
根据模板就可以在不同的子串题中进行应用,也不是为了死搬硬套,这个只是利用map解决的思路,像递归解决排列问题一样。
题二:最长可出现两次子串
int lengthOfLongestSubstringTwoDistinct(string s) {
vector<int> map(, );
int counter=, begin=, end=, d=;
while(end<s.size()){
if(map[s[end++]]++==) counter++;
while(counter>) if(map[s[begin++]]--==) counter--;
d=max(d, end-begin);
}
return d;
}
题三:最长无重复子串‘
输出长度
int lengthOfLongestSubstring(string s) {
vector<int> map(,);
int counter=, begin=, end=, d=;
while(end<s.size()){
if(map[s[end++]]++>) counter++;
while(counter>) if(map[s[begin++]]-->) counter--;
d=max(d, end-begin); //while valid, update d
}
return d;
}
输出具体子串,这里加一个子串起始标志位就行,在更新长度时更新子串起始位就行
【LeetCode】 子字符串思路的更多相关文章
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- [LeetCode] 647. Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- C#LeetCode刷题之#459-重复的子字符串(Repeated Substring Pattern)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3945 访问. 给定一个非空的字符串,判断它是否可以由它的一个子串 ...
- leetcode 1593. 拆分字符串使唯一子字符串的数目最大(DFS,剪枝)
题目链接 leetcode 1593. 拆分字符串使唯一子字符串的数目最大 题意: 给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目. 字符串 s 拆分后可以得到若干 非空子 ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- Leetcode 459.重复的子字符串
重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: ...
- LeetCode 459. 重复的子字符串(Repeated Substring Pattern)
459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...
- 【leetcode 简单】 第一百一十二题 重复的子字符串
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释 ...
随机推荐
- IRQ与FIQ的区别
1.对FIQ你必须进快处理中断请求,并离开这个模式. 2.IRQ可以被FIQ所中断,但FIQ不能被IRQ所中断,在处理FIQ时必须要关闭中断. 3.FIQ的优先级比IRQ高. 4.FIQ模式下,比IR ...
- PostgreSQL tips
tip 1 在sql中我们可以设置一个列自增长identity(1,1),但在postgresql中却没有这个关键字定义.但postgresql也有实现相关功能,那就是只需要将该列数据类型标记为ser ...
- Laravel5.1 模型--查询作用域
所谓的查询作用域就是允许你自定义一个查询语句 把它封装成一个方法. 1 定义一个查询作用域 定义查询作用域就是在模型中声明一个scope开头的方法: public function scopeHotA ...
- &&和&(||和|)区别
上代码,引出问题 public class Test { private static int j = 0; private static Boolean methodB(int k) { j += ...
- Linq中GroupBy方法的使用总结(转载)
from:https://www.cnblogs.com/zhouzangood/articles/4565466.html Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均 ...
- Ninject学习笔记<三>
ASP.NET MVC学前篇之Ninject的初步了解 1.介绍 废话几句,Ninject是一种轻量级的.基础.NET的一个开源IoC框架,在对于MVC框架的学习中会用到IoC框架的,因为这种IoC开 ...
- 学生成绩管理系统【c】
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #d ...
- svn 更新文件冲突,提示中文乱码解决
问题描述: update 操作提示错误信息,中文乱码 和 “Please execute the 'Cleanup' command.” Cleanup 操作报错: 解决办法: 1. 工具下载(sql ...
- 2588: Spoj 10628. Count on a tree
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5766 Solved: 1374 ...
- EasyNVR智能云终端硬件使用说明(EasyNVR无插件直播服务硬件的具体使用方法)
问题背景 随着EasyNVR硬件版本(EasyNVR硬件云终端)的发布不少客户选择了EasyNVR云终端作为产品选择,在客户收到EasyNVR云终端的时候肯定都有一个疑问,那就是如何使用手头上的这个小 ...