在一些求字串含有固定字符最短串,含有不同字符最长子串等问题中,利用 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.
Seen this question in a real interview before?

思路:

利用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】 子字符串思路的更多相关文章

  1. [LeetCode] Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  2. [LeetCode] 647. Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  3. C#LeetCode刷题之#459-重复的子字符串(Repeated Substring Pattern)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3945 访问. 给定一个非空的字符串,判断它是否可以由它的一个子串 ...

  4. leetcode 1593. 拆分字符串使唯一子字符串的数目最大(DFS,剪枝)

    题目链接 leetcode 1593. 拆分字符串使唯一子字符串的数目最大 题意: 给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目. 字符串 s 拆分后可以得到若干 非空子 ...

  5. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  7. Leetcode 459.重复的子字符串

    重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: ...

  8. LeetCode 459. 重复的子字符串(Repeated Substring Pattern)

    459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...

  9. 【leetcode 简单】 第一百一十二题 重复的子字符串

    给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释 ...

随机推荐

  1. Hibernate使用注意事项

    映射的POJO类型中如果有基本类型最好都是用包装类来代替: 注意关联关系的正确设置,一对一(主键方式.外键外键方式),一对多和多对多的配置,单向.双向的配置: 更新核心配置文件的mapping:如果是 ...

  2. 工业控制系统PLC、DCS、ESD

    PLC:可编程逻辑控制系统.PLC是一种专为在工业环境应用而设计的数字运算电子系统. DCS:集散控制系统. ESD:紧急停车系统.

  3. SecurCRT 远程linux 输入中文及 oracle 查询出文号问题

    一. 首先确认你的linux是否设置了支持中文 cat /etc/sysconfig/i18n 其中: LANG 变量是 language 的简称, 这个变量时决定系统的默认语言, 即系统菜单, 程序 ...

  4. htm5 俄罗斯方块

    <!DOCTYPE html> <html manifest="tetris.manifest"> <!--在HTML标签里manifest=”cac ...

  5. Python 之 ImportError: No module named ***

    假设想使用非当前模块中的代码,须要使用Import.这个大家都知道. 假设你要使用的模块(py文件)和当前模块在同一文件夹.仅仅要import对应的文件名称就好,比方在a.py中使用b.py: imp ...

  6. python3----基础 用while循环+iter()+next() 实现对字符串的遍历与输出

    my_str = 'hello' # for循环 for v in my_str: print(v) # while 配合迭代器实现字符串的遍历 ite = iter(my_str) while Tr ...

  7. 2204 Problem A(水)

    问题 A: [高精度]被限制的加法 时间限制: 1 Sec  内存限制: 16 MB 提交: 54  解决: 29 [提交][状态][讨论版] 题目描述 据关押修罗王和邪狼监狱的典狱长吹嘘,该监狱自一 ...

  8. CentOS6.X 升级内核至 3.10

    1.1 查看当前版本 [root@localhost ~]# uname -r -.el6.x86_64 1.2 导入public key [root@localhost ~]# rpm --impo ...

  9. 1、aritcMS-环境搭建-设置布局-布局测试

    在开始项目之前,准备开发工具等请参考easyUI章节. 首先.在获取broPHP框架的时候(可以在网上下载).在brophp文件夹同级目录下新建admin.php,以及index.php admin. ...

  10. Android Studio中debug和release模式默认的参数配置

    The possible properties and their default values are: debuggable:表示是否可以在手机上调试程序. 在Eclipse中,只有android ...