From 70b516c0fa39616cc0ea27279e0a3e6a9a5b6021 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 18 Aug 2024 21:58:53 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3258 --- .../README.md | 204 ++++++++++++++++++ .../README_EN.md | 202 +++++++++++++++++ .../Solution.cpp | 19 ++ .../Solution.go | 16 ++ .../Solution.java | 18 ++ .../Solution.py | 13 ++ .../Solution.ts | 15 ++ .../README.md | 103 +++++++++ .../README_EN.md | 103 +++++++++ .../README.md | 111 ++++++++++ .../README_EN.md | 109 ++++++++++ .../README.md | 108 ++++++++++ .../README_EN.md | 106 +++++++++ solution/CONTEST_README.md | 4 + solution/CONTEST_README_EN.md | 4 + solution/README.md | 4 + solution/README_EN.md | 4 + 17 files changed, 1143 insertions(+) create mode 100644 solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md create mode 100644 solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md create mode 100644 solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp create mode 100644 solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go create mode 100644 solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java create mode 100644 solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.py create mode 100644 solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts create mode 100644 solution/3200-3299/3259.Maximum Energy Boost From Two Drinks/README.md create mode 100644 solution/3200-3299/3259.Maximum Energy Boost From Two Drinks/README_EN.md create mode 100644 solution/3200-3299/3260.Find the Largest Palindrome Divisible by K/README.md create mode 100644 solution/3200-3299/3260.Find the Largest Palindrome Divisible by K/README_EN.md create mode 100644 solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README.md create mode 100644 solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README_EN.md diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md new file mode 100644 index 0000000000000..ec9250c5d7b95 --- /dev/null +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md @@ -0,0 +1,204 @@ +--- +comments: true +difficulty: 简单 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README.md +--- + + + +# [3258. 统计满足 K 约束的子字符串数量 I](https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i) + +[English Version](/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README_EN.md) + +## 题目描述 + + + +

给你一个 二进制 字符串 s 和一个整数 k

+ +

如果一个 二进制字符串 满足以下任一条件,则认为该字符串满足 k 约束

+ + + +

返回一个整数,表示 s 的所有满足 k 约束 子字符串的数量。

+ +

 

+ +

示例 1:

+ +
+

输入:s = "10101", k = 1

+ +

输出:12

+ +

解释:

+ +

s 的所有子字符串中,除了 "1010""10101""0101" 外,其余子字符串都满足 k 约束。

+
+ +

示例 2:

+ +
+

输入:s = "1010101", k = 2

+ +

输出:25

+ +

解释:

+ +

s 的所有子字符串中,除了长度大于 5 的子字符串外,其余子字符串都满足 k 约束。

+
+ +

示例 3:

+ +
+

输入:s = "11111", k = 1

+ +

输出:15

+ +

解释:

+ +

s 的所有子字符串都满足 k 约束。

+
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一:滑动窗口 + +我们用两个变量 $\textit{cnt0}$ 和 $\textit{cnt1}$ 分别记录当前窗口内的 $0$ 和 $1$ 的个数,用 $\textit{ans}$ 记录满足 $k$ 约束的子字符串的个数,用 $l$ 记录窗口的左边界。 + +当我们右移窗口时,如果窗口内的 $0$ 和 $1$ 的个数都大于 $k$,我们就需要左移窗口,直到窗口内的 $0$ 和 $1$ 的个数都不大于 $k$。此时,窗口内的所有子字符串都满足 $k$ 约束,个数为 $r - l + 1$,其中 $r$ 是窗口的右边界。我们将这个个数累加到 $\textit{ans}$ 中。 + +最后,我们返回 $\textit{ans}$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def countKConstraintSubstrings(self, s: str, k: int) -> int: + cnt0 = cnt1 = 0 + ans = l = 0 + for r, c in enumerate(s): + cnt0 += int(c) ^ 1 + cnt1 += int(c) + while cnt0 > k and cnt1 > k: + cnt0 -= int(s[l]) ^ 1 + cnt1 -= int(s[l]) + l += 1 + ans += r - l + 1 + return ans +``` + +#### Java + +```java +class Solution { + public int countKConstraintSubstrings(String s, int k) { + int cnt0 = 0, cnt1 = 0; + int ans = 0, l = 0; + for (int r = 0; r < s.length(); ++r) { + int x = s.charAt(r) - '0'; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + int y = s.charAt(l++) - '0'; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int countKConstraintSubstrings(string s, int k) { + int cnt0 = 0, cnt1 = 0; + int ans = 0, l = 0; + for (int r = 0; r < s.length(); ++r) { + int x = s[r] - '0'; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + int y = s[l++] - '0'; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; + } +}; +``` + +#### Go + +```go +func countKConstraintSubstrings(s string, k int) (ans int) { + cnt0, cnt1, l := 0, 0, 0 + for r, c := range s { + x := int(c - '0') + cnt0 += x ^ 1 + cnt1 += x + for cnt0 > k && cnt1 > k { + y := int(s[l] - '0') + cnt0 -= y ^ 1 + cnt1 -= y + l++ + } + ans += r - l + 1 + } + return +} +``` + +#### TypeScript + +```ts +function countKConstraintSubstrings(s: string, k: number): number { + let [cnt0, cnt1, ans, l] = [0, 0, 0, 0]; + for (let r = 0; r < s.length; ++r) { + const x = s[r] === '1' ? 1 : 0; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + const y = s[l++] === '1' ? 1 : 0; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; +} +``` + + + + + + diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md new file mode 100644 index 0000000000000..6184ebfd23b1d --- /dev/null +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md @@ -0,0 +1,202 @@ +--- +comments: true +difficulty: Easy +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README_EN.md +--- + + + +# [3258. Count Substrings That Satisfy K-Constraint I](https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-i) + +[中文文档](/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README.md) + +## Description + + + +

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

+ +

A binary string satisfies the k-constraint if either of the following conditions holds:

+ + + +

Return an integer denoting the number of substrings of s that satisfy the k-constraint.

+ +

 

+

Example 1:

+ +
+

Input: s = "10101", k = 1

+ +

Output: 12

+ +

Explanation:

+ +

Every substring of s except the substrings "1010", "10101", and "0101" satisfies the k-constraint.

+
+ +

Example 2:

+ +
+

Input: s = "1010101", k = 2

+ +

Output: 25

+ +

Explanation:

+ +

Every substring of s except the substrings with a length greater than 5 satisfies the k-constraint.

+
+ +

Example 3:

+ +
+

Input: s = "11111", k = 1

+ +

Output: 15

+ +

Explanation:

+ +

All substrings of s satisfy the k-constraint.

+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1: Sliding Window + +We use two variables $\textit{cnt0}$ and $\textit{cnt1}$ to record the number of $0$s and $1$s in the current window, respectively. We use $\textit{ans}$ to record the number of substrings that satisfy the $k$ constraint, and $l$ to record the left boundary of the window. + +When we move the window to the right, if the number of $0$s and $1$s in the window both exceed $k$, we need to move the window to the left until the number of $0$s and $1$s in the window are both no greater than $k$. At this point, all substrings in the window satisfy the $k$ constraint, and the number of such substrings is $r - l + 1$, where $r$ is the right boundary of the window. We add this count to $\textit{ans}$. + +Finally, we return $\textit{ans}$. + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def countKConstraintSubstrings(self, s: str, k: int) -> int: + cnt0 = cnt1 = 0 + ans = l = 0 + for r, c in enumerate(s): + cnt0 += int(c) ^ 1 + cnt1 += int(c) + while cnt0 > k and cnt1 > k: + cnt0 -= int(s[l]) ^ 1 + cnt1 -= int(s[l]) + l += 1 + ans += r - l + 1 + return ans +``` + +#### Java + +```java +class Solution { + public int countKConstraintSubstrings(String s, int k) { + int cnt0 = 0, cnt1 = 0; + int ans = 0, l = 0; + for (int r = 0; r < s.length(); ++r) { + int x = s.charAt(r) - '0'; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + int y = s.charAt(l++) - '0'; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int countKConstraintSubstrings(string s, int k) { + int cnt0 = 0, cnt1 = 0; + int ans = 0, l = 0; + for (int r = 0; r < s.length(); ++r) { + int x = s[r] - '0'; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + int y = s[l++] - '0'; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; + } +}; +``` + +#### Go + +```go +func countKConstraintSubstrings(s string, k int) (ans int) { + cnt0, cnt1, l := 0, 0, 0 + for r, c := range s { + x := int(c - '0') + cnt0 += x ^ 1 + cnt1 += x + for cnt0 > k && cnt1 > k { + y := int(s[l] - '0') + cnt0 -= y ^ 1 + cnt1 -= y + l++ + } + ans += r - l + 1 + } + return +} +``` + +#### TypeScript + +```ts +function countKConstraintSubstrings(s: string, k: number): number { + let [cnt0, cnt1, ans, l] = [0, 0, 0, 0]; + for (let r = 0; r < s.length; ++r) { + const x = s[r] === '1' ? 1 : 0; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + const y = s[l++] === '1' ? 1 : 0; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; +} +``` + + + + + + diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp new file mode 100644 index 0000000000000..0b90f0c7816b6 --- /dev/null +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int countKConstraintSubstrings(string s, int k) { + int cnt0 = 0, cnt1 = 0; + int ans = 0, l = 0; + for (int r = 0; r < s.length(); ++r) { + int x = s[r] - '0'; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + int y = s[l++] - '0'; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; + } +}; diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go new file mode 100644 index 0000000000000..fa3e015156a13 --- /dev/null +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go @@ -0,0 +1,16 @@ +func countKConstraintSubstrings(s string, k int) (ans int) { + cnt0, cnt1, l := 0, 0, 0 + for r, c := range s { + x := int(c - '0') + cnt0 += x ^ 1 + cnt1 += x + for cnt0 > k && cnt1 > k { + y := int(s[l] - '0') + cnt0 -= y ^ 1 + cnt1 -= y + l++ + } + ans += r - l + 1 + } + return +} diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java new file mode 100644 index 0000000000000..e5a6fe9aef08a --- /dev/null +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public int countKConstraintSubstrings(String s, int k) { + int cnt0 = 0, cnt1 = 0; + int ans = 0, l = 0; + for (int r = 0; r < s.length(); ++r) { + int x = s.charAt(r) - '0'; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + int y = s.charAt(l++) - '0'; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; + } +} diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.py b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.py new file mode 100644 index 0000000000000..94dd71f835f60 --- /dev/null +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.py @@ -0,0 +1,13 @@ +class Solution: + def countKConstraintSubstrings(self, s: str, k: int) -> int: + cnt0 = cnt1 = 0 + ans = l = 0 + for r, c in enumerate(s): + cnt0 += int(c) ^ 1 + cnt1 += int(c) + while cnt0 > k and cnt1 > k: + cnt0 -= int(s[l]) ^ 1 + cnt1 -= int(s[l]) + l += 1 + ans += r - l + 1 + return ans diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts new file mode 100644 index 0000000000000..b888e706cd966 --- /dev/null +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts @@ -0,0 +1,15 @@ +function countKConstraintSubstrings(s: string, k: number): number { + let [cnt0, cnt1, ans, l] = [0, 0, 0, 0]; + for (let r = 0; r < s.length; ++r) { + const x = s[r] === '1' ? 1 : 0; + cnt0 += x ^ 1; + cnt1 += x; + while (cnt0 > k && cnt1 > k) { + const y = s[l++] === '1' ? 1 : 0; + cnt0 -= y ^ 1; + cnt1 -= y; + } + ans += r - l + 1; + } + return ans; +} diff --git a/solution/3200-3299/3259.Maximum Energy Boost From Two Drinks/README.md b/solution/3200-3299/3259.Maximum Energy Boost From Two Drinks/README.md new file mode 100644 index 0000000000000..dc9ec9bfa87a8 --- /dev/null +++ b/solution/3200-3299/3259.Maximum Energy Boost From Two Drinks/README.md @@ -0,0 +1,103 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3259.Maximum%20Energy%20Boost%20From%20Two%20Drinks/README.md +--- + + + +# [3259. 超级饮料的最大强化能量](https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks) + +[English Version](/solution/3200-3299/3259.Maximum%20Energy%20Boost%20From%20Two%20Drinks/README_EN.md) + +## 题目描述 + + + +

来自未来的体育科学家给你两个整数数组 energyDrinkAenergyDrinkB,数组长度都等于 n。这两个数组分别代表 A、B 两种不同能量饮料每小时所能提供的强化能量。

+ +

你需要每小时饮用一种能量饮料来 最大化 你的总强化能量。然而,如果从一种能量饮料切换到另一种,你需要等待一小时来梳理身体的能量体系(在那个小时里你将不会获得任何强化能量)。

+ +

返回在接下来的 n 小时内你能获得的 最大 总强化能量。

+ +

注意 你可以选择从饮用任意一种能量饮料开始。

+ +

 

+ +

示例 1:

+ +
+

输入:energyDrinkA = [1,3,1], energyDrinkB = [3,1,1]

+ +

输出:5

+ +

解释:

+ +

要想获得 5 点强化能量,需要选择只饮用能量饮料 A(或者只饮用 B)。

+
+ +

示例 2:

+ +
+

输入:energyDrinkA = [4,1,1], energyDrinkB = [1,1,3]

+ +

输出:7

+ +

解释:

+ + +
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3200-3299/3259.Maximum Energy Boost From Two Drinks/README_EN.md b/solution/3200-3299/3259.Maximum Energy Boost From Two Drinks/README_EN.md new file mode 100644 index 0000000000000..9d3e37f4b7668 --- /dev/null +++ b/solution/3200-3299/3259.Maximum Energy Boost From Two Drinks/README_EN.md @@ -0,0 +1,103 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3259.Maximum%20Energy%20Boost%20From%20Two%20Drinks/README_EN.md +--- + + + +# [3259. Maximum Energy Boost From Two Drinks](https://leetcode.com/problems/maximum-energy-boost-from-two-drinks) + +[中文文档](/solution/3200-3299/3259.Maximum%20Energy%20Boost%20From%20Two%20Drinks/README.md) + +## Description + + + +

You are given two integer arrays energyDrinkA and energyDrinkB of the same length n by a futuristic sports scientist. These arrays represent the energy boosts per hour provided by two different energy drinks, A and B, respectively.

+ +

You want to maximize your total energy boost by drinking one energy drink per hour. However, if you want to switch from consuming one energy drink to the other, you need to wait for one hour to cleanse your system (meaning you won't get any energy boost in that hour).

+ +

Return the maximum total energy boost you can gain in the next n hours.

+ +

Note that you can start consuming either of the two energy drinks.

+ +

 

+

Example 1:

+ +
+

Input: energyDrinkA = [1,3,1], energyDrinkB = [3,1,1]

+ +

Output: 5

+ +

Explanation:

+ +

To gain an energy boost of 5, drink only the energy drink A (or only B).

+
+ +

Example 2:

+ +
+

Input: energyDrinkA = [4,1,1], energyDrinkB = [1,1,3]

+ +

Output: 7

+ +

Explanation:

+ +

To gain an energy boost of 7:

+ + +
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3200-3299/3260.Find the Largest Palindrome Divisible by K/README.md b/solution/3200-3299/3260.Find the Largest Palindrome Divisible by K/README.md new file mode 100644 index 0000000000000..ef06880541506 --- /dev/null +++ b/solution/3200-3299/3260.Find the Largest Palindrome Divisible by K/README.md @@ -0,0 +1,111 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3260.Find%20the%20Largest%20Palindrome%20Divisible%20by%20K/README.md +--- + + + +# [3260. 找出最大的 N 位 K 回文数](https://leetcode.cn/problems/find-the-largest-palindrome-divisible-by-k) + +[English Version](/solution/3200-3299/3260.Find%20the%20Largest%20Palindrome%20Divisible%20by%20K/README_EN.md) + +## 题目描述 + + + +

给你两个 正整数 nk

+ +

如果整数 x 满足以下全部条件,则该整数是一个 k 回文数

+ + + +

以字符串形式返回 最大的  nk 回文数

+ +

注意,该整数 含前导零。

+ +

 

+ +

示例 1:

+ +
+

输入: n = 3, k = 5

+ +

输出: "595"

+ +

解释:

+ +

595 是最大的 3 位 k 回文数。

+
+ +

示例 2:

+ +
+

输入: n = 1, k = 4

+ +

输出: "8"

+ +

解释:

+ +

1 位 k 回文数只有 4 和 8。

+
+ +

示例 3:

+ +
+

输入: n = 5, k = 6

+ +

输出: "89898"

+
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3200-3299/3260.Find the Largest Palindrome Divisible by K/README_EN.md b/solution/3200-3299/3260.Find the Largest Palindrome Divisible by K/README_EN.md new file mode 100644 index 0000000000000..0101e9258ee87 --- /dev/null +++ b/solution/3200-3299/3260.Find the Largest Palindrome Divisible by K/README_EN.md @@ -0,0 +1,109 @@ +--- +comments: true +difficulty: Hard +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3260.Find%20the%20Largest%20Palindrome%20Divisible%20by%20K/README_EN.md +--- + + + +# [3260. Find the Largest Palindrome Divisible by K](https://leetcode.com/problems/find-the-largest-palindrome-divisible-by-k) + +[中文文档](/solution/3200-3299/3260.Find%20the%20Largest%20Palindrome%20Divisible%20by%20K/README.md) + +## Description + + + +

You are given two positive integers n and k.

+ +

An integer x is called k-palindromic if:

+ + + +

Return the largest integer having n digits (as a string) that is k-palindromic.

+ +

Note that the integer must not have leading zeros.

+ +

 

+

Example 1:

+ +
+

Input: n = 3, k = 5

+ +

Output: "595"

+ +

Explanation:

+ +

595 is the largest k-palindromic integer with 3 digits.

+
+ +

Example 2:

+ +
+

Input: n = 1, k = 4

+ +

Output: "8"

+ +

Explanation:

+ +

4 and 8 are the only k-palindromic integers with 1 digit.

+
+ +

Example 3:

+ +
+

Input: n = 5, k = 6

+ +

Output: "89898"

+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README.md b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README.md new file mode 100644 index 0000000000000..424b72457134b --- /dev/null +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README.md @@ -0,0 +1,108 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3261.Count%20Substrings%20That%20Satisfy%20K-Constraint%20II/README.md +--- + + + +# [3261. 统计满足 K 约束的子字符串数量 II](https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-ii) + +[English Version](/solution/3200-3299/3261.Count%20Substrings%20That%20Satisfy%20K-Constraint%20II/README_EN.md) + +## 题目描述 + + + +

给你一个 二进制 字符串 s 和一个整数 k

+ +

另给你一个二维整数数组 queries ,其中 queries[i] = [li, ri]

+ +

如果一个 二进制字符串 满足以下任一条件,则认为该字符串满足 k 约束

+ + + +

返回一个整数数组 answer ,其中 answer[i] 表示 s[li..ri] 中满足 k 约束子字符串 的数量。

+ +

 

+ +

示例 1:

+ +
+

输入:s = "0001111", k = 2, queries = [[0,6]]

+ +

输出:[26]

+ +

解释:

+ +

对于查询 [0, 6]s[0..6] = "0001111" 的所有子字符串中,除 s[0..5] = "000111"s[0..6] = "0001111" 外,其余子字符串都满足 k 约束。

+
+ +

示例 2:

+ +
+

输入:s = "010101", k = 1, queries = [[0,5],[1,4],[2,3]]

+ +

输出:[15,9,3]

+ +

解释:

+ +

s 的所有子字符串中,长度大于 3 的子字符串都不满足 k 约束。

+
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README_EN.md b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README_EN.md new file mode 100644 index 0000000000000..f5c8ec13944a7 --- /dev/null +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README_EN.md @@ -0,0 +1,106 @@ +--- +comments: true +difficulty: Hard +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3261.Count%20Substrings%20That%20Satisfy%20K-Constraint%20II/README_EN.md +--- + + + +# [3261. Count Substrings That Satisfy K-Constraint II](https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-ii) + +[中文文档](/solution/3200-3299/3261.Count%20Substrings%20That%20Satisfy%20K-Constraint%20II/README.md) + +## Description + + + +

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

+ +

You are also given a 2D integer array queries, where queries[i] = [li, ri].

+ +

A binary string satisfies the k-constraint if either of the following conditions holds:

+ + + +

Return an integer array answer, where answer[i] is the number of substrings of s[li..ri] that satisfy the k-constraint.

+ +

 

+

Example 1:

+ +
+

Input: s = "0001111", k = 2, queries = [[0,6]]

+ +

Output: [26]

+ +

Explanation:

+ +

For the query [0, 6], all substrings of s[0..6] = "0001111" satisfy the k-constraint except for the substrings s[0..5] = "000111" and s[0..6] = "0001111".

+
+ +

Example 2:

+ +
+

Input: s = "010101", k = 1, queries = [[0,5],[1,4],[2,3]]

+ +

Output: [15,9,3]

+ +

Explanation:

+ +

The substrings of s with a length greater than 3 do not satisfy the k-constraint.

+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md index b0038c9a845bb..67976f67a5ab0 100644 --- a/solution/CONTEST_README.md +++ b/solution/CONTEST_README.md @@ -28,6 +28,10 @@ comments: true #### 第 411 场周赛(2024-08-18 10:30, 90 分钟) 参赛人数 2688 +- [3258. 统计满足 K 约束的子字符串数量 I](/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README.md) +- [3259. 超级饮料的最大强化能量](/solution/3200-3299/3259.Maximum%20Energy%20Boost%20From%20Two%20Drinks/README.md) +- [3260. 找出最大的 N 位 K 回文数](/solution/3200-3299/3260.Find%20the%20Largest%20Palindrome%20Divisible%20by%20K/README.md) +- [3261. 统计满足 K 约束的子字符串数量 II](/solution/3200-3299/3261.Count%20Substrings%20That%20Satisfy%20K-Constraint%20II/README.md) #### 第 137 场双周赛(2024-08-17 22:30, 90 分钟) 参赛人数 2198 diff --git a/solution/CONTEST_README_EN.md b/solution/CONTEST_README_EN.md index a0c87abbc8f69..4d933a032a96a 100644 --- a/solution/CONTEST_README_EN.md +++ b/solution/CONTEST_README_EN.md @@ -31,6 +31,10 @@ If you want to estimate your score changes after the contest ends, you can visit #### Weekly Contest 411 +- [3258. Count Substrings That Satisfy K-Constraint I](/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README_EN.md) +- [3259. Maximum Energy Boost From Two Drinks](/solution/3200-3299/3259.Maximum%20Energy%20Boost%20From%20Two%20Drinks/README_EN.md) +- [3260. Find the Largest Palindrome Divisible by K](/solution/3200-3299/3260.Find%20the%20Largest%20Palindrome%20Divisible%20by%20K/README_EN.md) +- [3261. Count Substrings That Satisfy K-Constraint II](/solution/3200-3299/3261.Count%20Substrings%20That%20Satisfy%20K-Constraint%20II/README_EN.md) #### Biweekly Contest 137 diff --git a/solution/README.md b/solution/README.md index 943ee35981a99..16a44f3ffa379 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3268,6 +3268,10 @@ | 3255 | [长度为 K 的子数组的能量值 II](/solution/3200-3299/3255.Find%20the%20Power%20of%20K-Size%20Subarrays%20II/README.md) | | 中等 | 第 137 场双周赛 | | 3256 | [放三个车的价值之和最大 I](/solution/3200-3299/3256.Maximum%20Value%20Sum%20by%20Placing%20Three%20Rooks%20I/README.md) | | 困难 | 第 137 场双周赛 | | 3257 | [放三个车的价值之和最大 II](/solution/3200-3299/3257.Maximum%20Value%20Sum%20by%20Placing%20Three%20Rooks%20II/README.md) | | 困难 | 第 137 场双周赛 | +| 3258 | [统计满足 K 约束的子字符串数量 I](/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README.md) | | 简单 | 第 411 场周赛 | +| 3259 | [超级饮料的最大强化能量](/solution/3200-3299/3259.Maximum%20Energy%20Boost%20From%20Two%20Drinks/README.md) | | 中等 | 第 411 场周赛 | +| 3260 | [找出最大的 N 位 K 回文数](/solution/3200-3299/3260.Find%20the%20Largest%20Palindrome%20Divisible%20by%20K/README.md) | | 困难 | 第 411 场周赛 | +| 3261 | [统计满足 K 约束的子字符串数量 II](/solution/3200-3299/3261.Count%20Substrings%20That%20Satisfy%20K-Constraint%20II/README.md) | | 困难 | 第 411 场周赛 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index d28b62c66df8f..f7b1122cf8297 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3266,6 +3266,10 @@ Press Control + F(or Command + F on | 3255 | [Find the Power of K-Size Subarrays II](/solution/3200-3299/3255.Find%20the%20Power%20of%20K-Size%20Subarrays%20II/README_EN.md) | | Medium | Biweekly Contest 137 | | 3256 | [Maximum Value Sum by Placing Three Rooks I](/solution/3200-3299/3256.Maximum%20Value%20Sum%20by%20Placing%20Three%20Rooks%20I/README_EN.md) | | Hard | Biweekly Contest 137 | | 3257 | [Maximum Value Sum by Placing Three Rooks II](/solution/3200-3299/3257.Maximum%20Value%20Sum%20by%20Placing%20Three%20Rooks%20II/README_EN.md) | | Hard | Biweekly Contest 137 | +| 3258 | [Count Substrings That Satisfy K-Constraint I](/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README_EN.md) | | Easy | Weekly Contest 411 | +| 3259 | [Maximum Energy Boost From Two Drinks](/solution/3200-3299/3259.Maximum%20Energy%20Boost%20From%20Two%20Drinks/README_EN.md) | | Medium | Weekly Contest 411 | +| 3260 | [Find the Largest Palindrome Divisible by K](/solution/3200-3299/3260.Find%20the%20Largest%20Palindrome%20Divisible%20by%20K/README_EN.md) | | Hard | Weekly Contest 411 | +| 3261 | [Count Substrings That Satisfy K-Constraint II](/solution/3200-3299/3261.Count%20Substrings%20That%20Satisfy%20K-Constraint%20II/README_EN.md) | | Hard | Weekly Contest 411 | ## Copyright