Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
- [乘积小于 K 的子数组](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README.md) - `双指针`
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - `位运算`、`lowbit`
- [合并区间](/solution/0000-0099/0056.Merge%20Intervals/README.md) - `区间合并`
<!-- 排序算法、待补充 -->
<!-- 排序算法、待补充 -->

### 2. 数据结构

Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3794.Reverse String Prefix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3794.Reverse%20String%20Prefix/README.md
rating: 1229
source: 第 173 场双周赛 Q1
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3794.Reverse String Prefix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3794.Reverse%20String%20Prefix/README_EN.md
rating: 1229
source: Biweekly Contest 173 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3795.Minimum%20Subarray%20Length%20With%20Distinct%20Sum%20At%20Least%20K/README.md
rating: 1504
source: 第 173 场双周赛 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3795.Minimum%20Subarray%20Length%20With%20Distinct%20Sum%20At%20Least%20K/README_EN.md
rating: 1504
source: Biweekly Contest 173 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3796.Find%20Maximum%20Value%20in%20a%20Constrained%20Sequence/README.md
rating: 1832
source: 第 173 场双周赛 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3796.Find%20Maximum%20Value%20in%20a%20Constrained%20Sequence/README_EN.md
rating: 1832
source: Biweekly Contest 173 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3797.Count%20Routes%20to%20Climb%20a%20Rectangular%20Grid/README.md
rating: 2375
source: 第 173 场双周赛 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3797.Count%20Routes%20to%20Climb%20a%20Rectangular%20Grid/README_EN.md
rating: 2375
source: Biweekly Contest 173 Q4
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3798.Largest Even Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3798.Largest%20Even%20Number/README.md
rating: 1365
source: 第 483 场周赛 Q1
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3798.Largest Even Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3798.Largest%20Even%20Number/README_EN.md
rating: 1365
source: Weekly Contest 483 Q1
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3799.Word Squares II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3799.Word%20Squares%20II/README.md
rating: 1606
source: 第 483 场周赛 Q2
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3799.Word Squares II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3799.Word%20Squares%20II/README_EN.md
rating: 1606
source: Weekly Contest 483 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3800.Minimum%20Cost%20to%20Make%20Two%20Binary%20Strings%20Equal/README.md
rating: 1840
source: 第 483 场周赛 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3800.Minimum%20Cost%20to%20Make%20Two%20Binary%20Strings%20Equal/README_EN.md
rating: 1840
source: Weekly Contest 483 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3801.Minimum%20Cost%20to%20Merge%20Sorted%20Lists/README.md
rating: 2398
source: 第 483 场周赛 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3801.Minimum%20Cost%20to%20Merge%20Sorted%20Lists/README_EN.md
rating: 2398
source: Weekly Contest 483 Q4
---

<!-- problem:start -->
Expand Down
190 changes: 190 additions & 0 deletions solution/3800-3899/3803.Count Residue Prefixes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3803.Count%20Residue%20Prefixes/README.md
---

<!-- problem:start -->

# [3803. 统计残差前缀](https://leetcode.cn/problems/count-residue-prefixes)

[English Version](/solution/3800-3899/3803.Count%20Residue%20Prefixes/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个仅由小写英文字母组成的字符串 <code>s</code>。</p>

<p>如果字符串 <code>s</code> 的某个&nbsp;<strong>前缀</strong>&nbsp;中<strong>&nbsp;不同字符的数量</strong>&nbsp;等于 <code>len(prefix) % 3</code>,则该前缀被称为<strong>残差前缀</strong>(residue)。</p>

<p>返回字符串 <code>s</code> 中<strong>&nbsp;残差前缀&nbsp;</strong>的数量。</p>

<p>字符串的<strong>&nbsp;前缀&nbsp;</strong>是一个&nbsp;<strong>非空子字符串</strong>,从字符串的开头起始并延伸到任意位置。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "abc"</span></p>

<p><strong>输出:</strong> <span class="example-io">2</span></p>

<p><strong>解释:</strong>​​​​​​​</p>

<ul>
<li>前缀 <code>"a"</code> 有 1 个不同字符,且长度模 3 为 1,因此它是一个残差前缀。</li>
<li>前缀 <code>"ab"</code> 有 2 个不同字符,且长度模 3 为 2,因此它是一个残差前缀。</li>
<li>前缀 <code>"abc"</code> 不满足条件,因此不是残差前缀。</li>
</ul>

<p>因此,答案是 2。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "dd"</span></p>

<p><strong>输出:</strong> <span class="example-io">1</span></p>

<p><strong>解释:</strong>​​​​​​​</p>

<ul>
<li>前缀 <code>"d"</code> 有 1 个不同字符,且长度模 3 为 1,因此它是一个残差前缀。</li>
<li>前缀 <code>"dd"</code> 有 1 个不同字符,但长度模 3 为 2,因此它不是残差前缀。</li>
</ul>

<p>因此,答案是 1。</p>
</div>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "bob"</span></p>

<p><strong>输出:</strong> <span class="example-io">2</span></p>

<p><strong>解释:</strong></p>

<ul>
<li>前缀 <code>"b"</code> 有 1 个不同字符,且长度模 3 为 1,因此它是一个残差前缀。</li>
<li>前缀 <code>"bo"</code> 有 2 个不同字符,且长度模 3 为 2,因此它是一个残差前缀。</li>
<li>前缀 <code>"bob"</code> 不满足条件。</li>
</ul>

<p>因此,答案是 2。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code> 仅包含小写英文字母。</li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python
class Solution:
def residuePrefixes(self, s: str) -> int:
st = set()
ans = 0
for i, c in enumerate(s, 1):
st.add(c)
if len(st) == i % 3:
ans += 1
return ans
```

#### Java

```java
class Solution {
public int residuePrefixes(String s) {
Set<Character> st = new HashSet<>();
int ans = 0;
for (int i = 1; i <= s.length(); i++) {
char c = s.charAt(i - 1);
st.add(c);
if (st.size() == i % 3) {
ans++;
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int residuePrefixes(string s) {
unordered_set<char> st;
int ans = 0;
for (int i = 1; i <= s.size(); i++) {
char c = s[i - 1];
st.insert(c);
if (st.size() == i % 3) {
ans++;
}
}
return ans;
}
};
```

#### Go

```go
func residuePrefixes(s string) int {
st := make(map[rune]struct{})
ans := 0
for i, c := range s {
idx := i + 1
st[c] = struct{}{}
if len(st) == idx%3 {
ans++
}
}
return ans
}
```

#### TypeScript

```ts
function residuePrefixes(s: string): number {
const st = new Set<string>();
let ans = 0;
for (let i = 0; i < s.length; i++) {
const c = s[i];
st.add(c);
if (st.size === (i + 1) % 3) {
ans++;
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading