【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 解释 ...
随机推荐
- java.lang.IllegalStateException: class utils.filter.ContentFilter is not a javax.servlet.Filter
1.错误描写叙述 2016-01-12 11:27:01.787:WARN:oejuc.AbstractLifeCycle:FAILED ContentFilter: java.lang.Illega ...
- JavaScript-4.5 事件大全,事件监听---ShinePans
绑定事件 <input type="bubtton" onclick="javascript:alert('I am clicked');"> 处理 ...
- JVM架构和GC垃圾回收机制详解
JVM架构图分析 下图:参考网络+书籍,如有侵权请见谅 (想了解Hadoop内存溢出请看:Hadoop内存溢出(OOM)分类.参数调优化) JVM被分为三个主要的子系统 (1)类加载器子系统(2)运行 ...
- TP3.2 引入微信类
首先建立一个入口IndexController.php文件 <?php namespace Home\Controller; use Think\Controller; use Com\Wech ...
- Java凝视分类
Java凝视分类 1.单行凝视 //打印结果 System.out.println("结果是:"+result); 2.多行凝视 /** * @autho ...
- ubuntu中怎样添加或删除一个PPA源
添加PPA源的命令为:sudo add-apt-repository ppa:user/ppa-name 添加好更新一下: sudo apt-get update删除命令格式则为:sudo add-a ...
- 一个改动配置文件的linux shell script
不久以前,以前搜到一篇博客是读取配置文件的,http://www.cnblogs.com/bo083/archive/2012/11/19/2777076.html,用到如今,感觉十分方便.感谢作者. ...
- Codeforces 455A Boredom 取数字的dp
题目链接:点击打开链接 给定一个n长的序列 删除x这个数就能获得x * x的个数 的分数,然后x+1和x-1这2个数会消失.即无法获得这2个数的分数 问最高得分. 先统计每一个数出现的次数.然后dp一 ...
- ASP.NET动态网站制作(30)-- WEBService
前言:继续讲正则表达式,然后介绍一下webservice. 内容: 1.匹配QQ号的正则表达式:^[1-9]\d{4,10}$:匹配手机号的正则表达式:^(0|86)?(13|14|15|18)[0- ...
- javascript中字符串截取的两种方法
var testStr = "hello kay!"; 1.substr testStr.substr(1) ->ello kay! testStr.substr(1,4 ...