Sliding Window

Sliding Window

Thirty-five questions on the sliding window technique, for solving subarray and substring problems in linear time.

Alternating Groups I

easy
There is a circle of red and blue tiles.

Contains Duplicate II

easy
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.

Count Substrings That Satisfy K-Constraint I

easy
You are given a binary string s and an integer k.

Defuse the Bomb

easy
You have a bomb to defuse, and your time is running out!

Find the K-Beauty of a Number

easy
The k-beauty of an integer num is defined as the number of substrings of num when it is read as a string that meet the following conditions: Given integers num and k, return the...

Find X-Sum of All K-Long Subarrays I

easy
You are given an array nums of n integers and two integers k and x.

Longest Even Odd Subarray With Threshold

easy
You are given a 0-indexed integer array nums and an integer threshold.

Longest Harmonious Subsequence

easy
We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1.

Longest Nice Substring

easy
A string s is nice if, for every letter of the alphabet that s contains, it appears both in uppercase and lowercase.

Maximum Average Subarray I

easy
You are given an integer array nums consisting of n elements, and an integer k.

Maximum Strong Pair XOR I

easy
You are given a 0-indexed integer array nums.

Maximum Subarray With Equal Products

easy
You are given an array of positive integers nums.

Minimum Difference Between Highest and Lowest of K Scores

easy
You are given a 0-indexed integer array nums, where nums[i] represents the score of the ith student.

Minimum Positive Sum Subarray

easy
You are given an integer array nums and two integers l and r.

Minimum Recolors to Get K Consecutive Black Blocks

easy
You are given a 0-indexed string blocks of length n, where blocks[i] is either 'W' or 'B', representing the color of the ith block.

Shortest Subarray With OR at Least K I

easy
You are given an array nums of non-negative integers and an integer k.

Substrings of Size Three with Distinct Characters

easy
A string is good if there are no repeated characters.

Alternating Groups II

medium
There is a circle of red and blue tiles.

Arithmetic Slices

medium
An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

Binary String With Substrings Representing 1 To N

medium
Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise.

Binary Subarrays With Sum

medium
Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal.

Continuous Subarrays

medium
You are given a 0-indexed integer array nums.

Count Complete Subarrays in an Array

medium
You are given an array nums consisting of positive integers.

Count Number of Nice Subarrays

medium
Given an array of integers nums and an integer k.

Count of Substrings Containing Every Vowel and K Consonants I

medium
You are given a string word and a non-negative integer k.

Count of Substrings Containing Every Vowel and K Consonants II

medium
You are given a string word and a non-negative integer k.

Count Partitions With Max-Min Difference at Most K

medium
You are given an integer array nums and an integer k.

Count Prime-Gap Balanced Subarrays

medium
You are given an integer array nums and an integer k.

Count Subarrays Where Max Element Appears at Least K Times

medium
You are given an integer array nums and a positive integer k.

Count Substrings That Can Be Rearranged to Contain a String I

medium
You are given two strings word1 and word2.

Count Substrings With K-Frequency Characters I

medium
Given a string s and an integer k, return the total number of substrings of s where at least one character appears at least k times.

Count the Number of Good Subarrays

medium
Given an integer array nums and an integer k, return the number of good subarrays of nums.

Count the Number of Substrings With Dominant Ones

medium
You are given a binary string s.

Count Zero Request Servers

medium
You are given an integer n denoting the total number of servers and a 2D 0-indexed integer array logs, where logs[i] = [server_id, time] denotes that the server with id server_id...

Find All Anagrams in a String

medium
Given two strings s and p, return an array of all the start indices of p's anagrams in s.