子字符串模板 (双指针, 滑动窗口)

? 对于大多数子字符串问题,我们给了一个字符串,需要找到一个满足某些限制的子字符串。通常的方法是使用带有两个指针的哈希表。模板如下。

public int findSubstring(string s){
        Map<Character, Integer> map = new HashMap<>():
        //也可用256长度的数组代替, int[] map = new int[256];
        int counter;                            // check whether the substring is valid
        int begin=0, end=0;                     //two pointers, one point to tail and one head
        int subLength;                          //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 subLength here if finding minimum*/

                //increase begin to make it invalid/valid again

                if (map[s[begin++]]++ ?) {
                    /*modify counter here*/
                }
            }  

            /* update subLength here if finding maximum*/
        }
        return subLength;
  }

? 需要提到的一件事是,当要求找到最大子串时,我们应该在内部while循环之后更新最大值,以确保子串有效。另一方面,当要求找到最小子串时,我们应该在内部while循环内更??新最小。

原文地址:https://www.cnblogs.com/willwuss/p/12276116.html

时间: 2024-08-02 08:09:13

子字符串模板 (双指针, 滑动窗口)的相关文章

leetcode ---双指针+滑动窗口

一:Minimum Size Subarray Sum 题目: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, th

LeetCode最小窗口子字符串以及求解子字符串模板

LeetCode原题地址 题干: 给定字符串S和T,以O(n)复杂度在S中找出包含T中所有字符的最小窗口子字符串. 示例: S = "ADOBECODEBANC"T = "ABC" 最小窗口是"BANC". 备注: 如果没有结果,返回空字符串"". 如果有多个窗口,保证只有一个最小解. Discuss中的答案(并且给出了一个求解所有子字符串问题的模板): 原题的解: 1 string minWindow(string s, s

LeetCode | 1358. Number of Substrings Containing All Three Characters包含所有三种字符的子字符串数目【Python】

LeetCode 1358. Number of Substrings Containing All Three Characters包含所有三种字符的子字符串数目[Medium][Python][双指针][滑窗] Problem LeetCode Given a string s consisting only of characters a, b and c. Return the number of substrings containing at least one occurrence

关于双端队列 deque 模板 &amp;&amp; 滑动窗口 (自出)

嗯... deque 即为双端队列,是c++语言中STL库中提供的一个东西,其功能比队列更强大,可以从队列的头与尾进行操作... 但是它的操作与队列十分相似,详见代码1: 1 #include <cstdio> 2 #include <iostream> 3 #include <deque> 4 //实际上,引用queue头文件也可以,里面包含了deque头文件 5 6 using namespace std; 7 8 deque<int> dq; //定义

AcWing 154. 滑动窗口(模板)

(https://www.acwing.com/problem/content/156/) 给定一个大小为n≤106n≤106的数组. 有一个大小为k的滑动窗口,它从数组的最左边移动到最右边. 您只能在窗口中看到k个数字. 每次滑动窗口向右移动一个位置. 以下是一个例子: 该数组为[1 3 -1 -3 5 3 6 7],k为3. 窗口位置 最小值 最大值 [1 3 -1] -3 5 3 6 7 -1 3 1 [3 -1 -3] 5 3 6 7 -3 3 1 3 [-1 -3 5] 3 6 7 -

Acwing 154 滑动窗口(单调队列)经典模板

给定一个大小为n≤106n≤106的数组. 有一个大小为k的滑动窗口,它从数组的最左边移动到最右边. 您只能在窗口中看到k个数字. 每次滑动窗口向右移动一个位置. 以下是一个例子: 该数组为[1 3 -1 -3 5 3 6 7],k为3. 窗口位置 最小值 最大值 [1 3 -1] -3 5 3 6 7 -1 3 1 [3 -1 -3] 5 3 6 7 -3 3 1 3 [-1 -3 5] 3 6 7 -3 5 1 3 -1 [-3 5 3] 6 7 -3 5 1 3 -1 -3 [5 3 6]

string类问题之滑动窗口类型

Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the

5325包含所有三种字符的子字符串数目

题目:给你一个字符串 s ,它只包含三种字符 a, b 和 c .请你返回 a,b 和 c 都 至少 出现过一次的子字符串数目. 链接:https://leetcode-cn.com/problems/number-of-substrings-containing-all-three-characters/ 法一:自己的代码 思路:刚开始的思路是利用类似戳气球的方法,dp[i][j]表示字符串中从i到j的符合条件的字符串的个数,进而推导从dp[i+1][j]和dp[i][j-1]如何计算出dp[

剑指offer-最长不含重复字符的子字符串-JavaScript

题目描述:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度. 题目分析 留意最长子串和子序列不是一个概念.例如对"pwwkew"来说,最长子串是"wke","pwke"是其中一个子序列. 在不考虑时间的情况下,直接暴力法对所有的子串进行检查.复杂度是\(O(N^3)\),会超时错误. 解法 1: 滑动窗口 准备 2 个指针 i.j,i 指向窗口左边,j 指向右边.指针每次可以向前"滑动"一个位置,它