Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8370854

Browse files
committed
Deploying to main from @ 2xiao/leetcode-js@d8ea13e 🚀
1 parent 5d9fa84 commit 8370854

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1180
-616
lines changed

‎book/slide_window.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ var lengthOfLongestSubstring = function (s) {
589589
| 992 | K 个不同整数的子数组 | | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`计数`](/tag/counting.md) `1+` | 🔴 | [🀄️](https://leetcode.cn/problems/subarrays-with-k-different-integers) [🔗](https://leetcode.com/problems/subarrays-with-k-different-integers) |
590590
| 713 | 乘积小于 K 的子数组 | | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`前缀和`](/tag/prefix-sum.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/subarray-product-less-than-k) [🔗](https://leetcode.com/problems/subarray-product-less-than-k) |
591591
| 904 | 水果成篮 | | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/fruit-into-baskets) [🔗](https://leetcode.com/problems/fruit-into-baskets) |
592-
| 1358 | 包含所有三种字符的子字符串数目 | | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters) [🔗](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters) |
592+
| 1358 | 包含所有三种字符的子字符串数目 | [[]](/problem/1358.md) | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters) [🔗](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters) |
593593
| 467 | 环绕字符串中唯一的子字符串 | [[]](/problem/0467.md) | [`字符串`](/tag/string.md) [`动态规划`](/tag/dynamic-programming.md) | 🟠 | [🀄️](https://leetcode.cn/problems/unique-substrings-in-wraparound-string) [🔗](https://leetcode.com/problems/unique-substrings-in-wraparound-string) |
594594
| 1438 | 绝对差不超过限制的最长连续子数组 | | [`队列`](/tag/queue.md) [`数组`](/tag/array.md) [`有序集合`](/tag/ordered-set.md) `3+` | 🟠 | [🀄️](https://leetcode.cn/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) [🔗](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) |
595595

‎plan/contest_list.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ headerDepth: 0
6161
| :------: | :------ | :------: | :------ | :------: | :------: |
6262
| 3304 | 找出第 K 个字符 I | | [`位运算`](/tag/bit-manipulation.md) [`递归`](/tag/recursion.md) [`数学`](/tag/math.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/find-the-k-th-character-in-string-game-i) [🔗](https://leetcode.com/problems/find-the-k-th-character-in-string-game-i) |
6363
| 3305 | 元音辅音字符串计数 I | | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i) [🔗](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i) |
64-
| 3306 | 元音辅音字符串计数 II | | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii) [🔗](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii) |
64+
| 3306 | 元音辅音字符串计数 II | [[✓]](/problem/3306.md) | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii) [🔗](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii) |
6565
| 3307 | 找出第 K 个字符 II | | [`位运算`](/tag/bit-manipulation.md) [`递归`](/tag/recursion.md) [`数学`](/tag/math.md) | 🔴 | [🀄️](https://leetcode.cn/problems/find-the-k-th-character-in-string-game-ii) [🔗](https://leetcode.com/problems/find-the-k-th-character-in-string-game-ii) |
6666

6767

@@ -289,7 +289,7 @@ headerDepth: 0
289289
| :------: | :------ | :------: | :------ | :------: | :------: |
290290
| 3206 | 交替组 I | | [`数组`](/tag/array.md) [`滑动窗口`](/tag/sliding-window.md) | 🟢 | [🀄️](https://leetcode.cn/problems/alternating-groups-i) [🔗](https://leetcode.com/problems/alternating-groups-i) |
291291
| 3207 | 与敌人战斗后的最大分数 | | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) | 🟠 | [🀄️](https://leetcode.cn/problems/maximum-points-after-enemy-battles) [🔗](https://leetcode.com/problems/maximum-points-after-enemy-battles) |
292-
| 3208 | 交替组 II | | [`数组`](/tag/array.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/alternating-groups-ii) [🔗](https://leetcode.com/problems/alternating-groups-ii) |
292+
| 3208 | 交替组 II | [[✓]](/problem/3208.md) | [`数组`](/tag/array.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/alternating-groups-ii) [🔗](https://leetcode.com/problems/alternating-groups-ii) |
293293
| 3209 | 子数组按位与值为 K 的数目 | | [`位运算`](/tag/bit-manipulation.md) [`线段树`](/tag/segment-tree.md) [`数组`](/tag/array.md) `1+` | 🔴 | [🀄️](https://leetcode.cn/problems/number-of-subarrays-with-and-value-of-k) [🔗](https://leetcode.com/problems/number-of-subarrays-with-and-value-of-k) |
294294

295295

@@ -1679,7 +1679,7 @@ headerDepth: 0
16791679
<!-- prettier-ignore -->
16801680
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
16811681
| :------: | :------ | :------: | :------ | :------: | :------: |
1682-
| 2529 | 正整数和负整数的最大计数 | | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`计数`](/tag/counting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer) [🔗](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) |
1682+
| 2529 | 正整数和负整数的最大计数 | [[✓]](/problem/2529.md) | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`计数`](/tag/counting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer) [🔗](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) |
16831683
| 2530 | 执行 K 次操作后的最大分数 | [[✓]](/problem/2530.md) | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`堆(优先队列)`](/tag/heap-priority-queue.md) | 🟠 | [🀄️](https://leetcode.cn/problems/maximal-score-after-applying-k-operations) [🔗](https://leetcode.com/problems/maximal-score-after-applying-k-operations) |
16841684
| 2531 | 使字符串中不同字符的数目相等 | | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`计数`](/tag/counting.md) | 🟠 | [🀄️](https://leetcode.cn/problems/make-number-of-distinct-characters-equal) [🔗](https://leetcode.com/problems/make-number-of-distinct-characters-equal) |
16851685
| 2532 | 过桥的时间 | | [`数组`](/tag/array.md) [`模拟`](/tag/simulation.md) [`堆(优先队列)`](/tag/heap-priority-queue.md) | 🔴 | [🀄️](https://leetcode.cn/problems/time-to-cross-a-bridge) [🔗](https://leetcode.com/problems/time-to-cross-a-bridge) |
@@ -4393,7 +4393,7 @@ headerDepth: 0
43934393
| :------: | :------ | :------: | :------ | :------: | :------: |
43944394
| 1356 | 根据数字二进制下 1 的数目排序 | [[✓]](/problem/1356.md) | [`位运算`](/tag/bit-manipulation.md) [`数组`](/tag/array.md) [`计数`](/tag/counting.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/sort-integers-by-the-number-of-1-bits) [🔗](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits) |
43954395
| 1357 | 每隔 n 个顾客打折 | | [`设计`](/tag/design.md) [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) | 🟠 | [🀄️](https://leetcode.cn/problems/apply-discount-every-n-orders) [🔗](https://leetcode.com/problems/apply-discount-every-n-orders) |
4396-
| 1358 | 包含所有三种字符的子字符串数目 | | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters) [🔗](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters) |
4396+
| 1358 | 包含所有三种字符的子字符串数目 | [[✓]](/problem/1358.md) | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md) | 🟠 | [🀄️](https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters) [🔗](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters) |
43974397
| 1359 | 有效的快递序列数目 | | [`数学`](/tag/math.md) [`动态规划`](/tag/dynamic-programming.md) [`组合数学`](/tag/combinatorics.md) | 🔴 | [🀄️](https://leetcode.cn/problems/count-all-valid-pickup-and-delivery-options) [🔗](https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options) |
43984398

43994399

‎problem/0121.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ cannot achieve any profit, return `0`.
7979

8080
### 思路一:动态规划
8181

82-
1. **动态规划:**定义一个二维数组 `dp`,其中 `dp[i][0]` 表示第 `i` 天不持有股票时的最大利润, `dp[i][1]` 表示第 `i` 天持有股票时的最大利润。
82+
1. **动态规划:**定义一个二维数组 `dp`,其中 `dp[i][0]` 表示第 `i` 天不持有股票时的最大利润, `dp[i][1]` 表示第 `i` 天持有股票时的最大利润。
8383
2. **状态转移方程:**
8484

8585
- `dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])`,表示在第 `i` 天,不持有股票的最大利润等于前一天不持有股票的最大利润,或者前一天持有股票的最大利润加上今天卖出的利润的最大值。
8686
- `dp[i][1] = max(dp[i-1][1], -prices[i])`,表示在第 `i` 天,持有股票的最大利润等于前一天持有股票的最大利润,或者前一天不持有股票的最大利润减去今天买入的利润的最大值。由于题目规定只能买入一次,所以前一天不持有股票的最大利润为 0。
8787

88-
3. **边界条件:**第一天(`i == 0`)时,`dp[0][0] = 0`,`dp[0][1] = -prices[0]`
89-
4. **初始化:**初始化利润为 0。
90-
5. **返回最大利润:**最后返回 `dp[n - 1][0]`,即最后一天不持有股票的最大利润,因为若最后一天还持有股票没有卖出,收益肯定小于做了一次交易的情况。
88+
3. **边界条件:**第一天(`i == 0`)时,`dp[0][0] = 0`,`dp[0][1] = -prices[0]`
89+
4. **初始化:**初始化利润为 0。
90+
5. **返回最大利润:**最后返回 `dp[n - 1][0]`,即最后一天不持有股票的最大利润,因为若最后一天还持有股票没有卖出,收益肯定小于做了一次交易的情况。
9191

9292
#### 复杂度分析
9393

‎problem/0494.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,15 @@ var findTargetSumWays = function (nums, target) {
225225
const sum = nums.reduce((num, acc) => num + acc, 0);
226226
if (sum < target || -sum > target || (sum + target) % 2 == 1) return 0;
227227

228-
const subset = (nums, sum) => {
229-
const dp = new Array(sum + 1).fill(0);
230-
dp[0] = 1;
231-
for (let num of nums) {
232-
for (let j = sum; j >= 0; j--) {
233-
if (j >= num) {
234-
dp[j] = dp[j - num] + dp[j];
235-
}
236-
}
228+
const n = (sum + target) / 2;
229+
const dp = new Array(n + 1).fill(0);
230+
dp[0] = 1;
231+
for (let num of nums) {
232+
for (let i = n; i >= num; i--) {
233+
dp[i] += dp[i - num];
237234
}
238-
return dp[sum];
239-
};
240-
return subset(nums, (sum + target) / 2);
235+
}
236+
return dp[n];
241237
};
242238
```
243239

‎problem/0704.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,4 @@ var search = function (nums, target) {
100100
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
101101
| :------: | :------ | :------: | :------ | :------: | :------: |
102102
| 702 | 搜索长度未知的有序数组 🔒 | | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`交互`](/tag/interactive.md) | 🟠 | [🀄️](https://leetcode.cn/problems/search-in-a-sorted-array-of-unknown-size) [🔗](https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size) |
103-
| 2529 | 正整数和负整数的最大计数 | | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`计数`](/tag/counting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer) [🔗](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) |
103+
| 2529 | 正整数和负整数的最大计数 | [[]](/problem/2529.md) | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`计数`](/tag/counting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer) [🔗](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) |

‎problem/1351.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,4 @@ var countNegatives = function (grid) {
166166
<!-- prettier-ignore -->
167167
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
168168
| :------: | :------ | :------: | :------ | :------: | :------: |
169-
| 2529 | 正整数和负整数的最大计数 | | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`计数`](/tag/counting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer) [🔗](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) |
169+
| 2529 | 正整数和负整数的最大计数 | [[]](/problem/2529.md) | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`计数`](/tag/counting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer) [🔗](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) |

‎problem/1358.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
title: 1358. 包含所有三种字符的子字符串数目
3+
description: LeetCode 1358. 包含所有三种字符的子字符串数目题解,Number of Substrings Containing All Three Characters,包含解题思路、复杂度分析以及完整的 JavaScript 代码实现。
4+
keywords:
5+
- LeetCode
6+
- 1358. 包含所有三种字符的子字符串数目
7+
- 包含所有三种字符的子字符串数目
8+
- Number of Substrings Containing All Three Characters
9+
- 解题思路
10+
- 哈希表
11+
- 字符串
12+
- 滑动窗口
13+
---
14+
15+
# 1358. 包含所有三种字符的子字符串数目
16+
17+
🟠 <font color=#ffb800>Medium</font>&emsp; 🔖&ensp; [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md)&emsp; 🔗&ensp;[`力扣`](https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters) [`LeetCode`](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters)
18+
19+
## 题目
20+
21+
Given a string `s` consisting only of characters a , b and c.
22+
23+
Return the number of substrings containing **at least** one occurrence of all
24+
these characters a , b and c.
25+
26+
**Example 1:**
27+
28+
> Input: s = "abcabc"
29+
>
30+
> Output: 10
31+
>
32+
> Explanation: The substrings containing at least one occurrence of the characters a , b and c are "abc" , "abca" , "abcab" , "abcabc" , "bca" , "bcab " , "bcabc" , "cab" , "cabc" and "abc" (**again**).
33+
34+
**Example 2:**
35+
36+
> Input: s = "aaacb"
37+
>
38+
> Output: 3
39+
>
40+
> Explanation: The substrings containing at least one occurrence of the characters a , b and c are "aaacb " , "aacb " and "acb ".
41+
42+
**Example 3:**
43+
44+
> Input: s = "abc"
45+
>
46+
> Output: 1
47+
48+
**Constraints:**
49+
50+
- `3 <= s.length <= 5 x 10^4`
51+
- `s` only consists of a , b or c characters.
52+
53+
## 题目大意
54+
55+
给你一个字符串 `s` ,它只包含三种字符 a, b 和 c 。
56+
57+
请你返回 a,b 和 c 都 **至少 **出现过一次的子字符串数目。
58+
59+
**示例 1:**
60+
61+
> **输入:** s = "abcabc"
62+
>
63+
> **输出:** 10
64+
>
65+
> **解释:** 包含 a,b 和 c 各至少一次的子字符串为 "abc " , "abca " , "abcab " , "abcabc " , "bca " , "bcab " , "bcabc " , "cab " , "cabc " 和 "abc " (**相同**字符串算多次)。
66+
67+
**示例 2:**
68+
69+
> **输入:** s = "aaacb"
70+
>
71+
> **输出:** 3
72+
>
73+
> **解释:** 包含 a,b 和 c 各至少一次的子字符串为 "aaacb " , "aacb " 和 "acb " 。
74+
75+
**示例 3:**
76+
77+
> **输入:** s = "abc"
78+
>
79+
> **输出:** 1
80+
81+
**提示:**
82+
83+
- `3 <= s.length <= 5 x 10^4`
84+
- `s` 只包含字符 a,b 和 c 。
85+
86+
## 解题思路
87+
88+
1. 使用 **滑动窗口** (`l``r`) 遍历 `s`,并维护一个 **固定大小的数组 `count[3]` 记录当前窗口内 `a, b, c` 出现的次数**
89+
90+
2. **右边界扩展窗口 (`r``0``n-1`)**
91+
92+
- `count[s[r]]` 计数 +1,表示字符 `s[r]` 进入窗口。
93+
94+
3. **左边界缩小窗口 (`l++`)**
95+
96+
-`count[0] > 0 && count[1] > 0 && count[2] > 0`(即窗口内包含 `a, b, c`)时:
97+
- `count[s[l]]` 计数 -1,`l++` 移动左边界。
98+
99+
4. **统计符合条件的子字符串**
100+
101+
- 缩小左边界直到窗口内不再同时包含 `a, b, c`,此时 `l` 代表有多少个可行的子字符串
102+
- `result += l`,计算所有可能的子字符串。
103+
104+
#### 复杂度分析
105+
106+
- **时间复杂度**:`O(n)`,遍历 `s`
107+
- **空间复杂度**:`O(1)`,只是用了常数个变量。
108+
109+
## 代码
110+
111+
```javascript
112+
/**
113+
* @param {string} s
114+
* @return {number}
115+
*/
116+
var numberOfSubstrings = function (s) {
117+
let l = 0;
118+
let result = 0;
119+
let count = [0, 0, 0];
120+
for (let r = 0; r < s.length; r++) {
121+
count[s.charCodeAt(r) - 97]++;
122+
// 只有当 'a', 'b', 'c' 都出现后,才移动左指针
123+
while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
124+
count[s.charCodeAt(l) - 97]--;
125+
l++;
126+
}
127+
result += l;
128+
}
129+
return result;
130+
};
131+
```
132+
133+
## 相关题目
134+
135+
<!-- prettier-ignore -->
136+
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
137+
| :------: | :------ | :------: | :------ | :------: | :------: |
138+
| 2063 | 所有子字符串中的元音 | | [`数学`](/tag/math.md) [`字符串`](/tag/string.md) [`动态规划`](/tag/dynamic-programming.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/vowels-of-all-substrings) [🔗](https://leetcode.com/problems/vowels-of-all-substrings) |
139+
| 2953 | 统计完全子字符串 | | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`滑动窗口`](/tag/sliding-window.md) | 🔴 | [🀄️](https://leetcode.cn/problems/count-complete-substrings) [🔗](https://leetcode.com/problems/count-complete-substrings) |

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /