【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 解释 ...
随机推荐
- 模式识别 - libsvm的函数调用方法 具体解释
libsvm的函数调用方法 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26261173 须要载入(load)SVM的 ...
- python中SQL的使用
# 常用的关系型数据库有 mysql postgresql sqlite 等(具体区别上课再说) # # 传统数据库以表的形式存储数据 # 一张表可以有很多个字段 # 以用户表为例, 存储 4 个数据 ...
- plsql programming 13 其他数据类型
bolean 类型 raw 类型, 用来保存和操作少量的二进制数据. urowid 和 rowid 类型, 这两种数据类型表示数据库的 rowid. 所谓 rowid 就是一个标识符-用来表示数据库中 ...
- Android实现录屏直播(二)需求才是硬道理之产品功能调研
请尊重分享成果,转载请注明出处,本文来自Coder包子哥,原文链接:http://blog.csdn.net/zxccxzzxz/article/details/54254244 前面的Android ...
- Python - json和simplejson比较(转)
From:https://stackoverflow.com/questions/712791/what-are-the-differences-between-json-and-simplejson ...
- Spring MVC密码处理
以下示例显示如何在使用Spring Web MVC框架的表单中使用密码.首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framework开发基于动态表单的 ...
- python django -4 模板
模板介绍 作为Web框架,Django提供了模板,可以很便利的动态生成HTML 模版系统致力于表达外观,而不是程序逻辑 模板的设计实现了业务逻辑(view)与显示内容(template)的分离,一个视 ...
- 一个简单的servlet容器
[0]README 0.1)本文部分文字转自 “深入剖析Tomcat”,旨在学习 一个简单的servlet容器 的基础知识: 0.2)for complete source code, pleas ...
- AndroidHttpClient和HttpEntity详解
AndroidHttpClient结构: public final class AndroidHttpClient extends Object implements HttpClient 前言:这类 ...
- poj 3469(网络流模版)
题目链接:http://poj.org/problem?id=3469 思路:终于把网络流的模版测试好了,在Dinic和Sap之间还是选择了Sap,事实证明Sap确实比Dinic效率高,在此贴出自己的 ...