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 dd425b0

Browse files
committed
Create 1876. 长度为三且各字符不同的子字符串.md
1 parent 84e4c3b commit dd425b0

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [1876. 长度为三且各字符不同的子字符串](https://leetcode.cn/problems/substrings-of-size-three-with-distinct-characters/description/)
2+
3+
- 标签:哈希表、字符串、计数、滑动窗口
4+
- 难度:简单
5+
6+
## 题目大意
7+
8+
**描述**:给定搞一个字符串 `s`
9+
10+
**要求**:返回 `s` 中长度为 3ドル$ 的好子字符串的数量。如果相同的好子字符串出现多次,则每一次都应该被记入答案之中。
11+
12+
**说明**:
13+
14+
- **子字符串**:指的是一个字符串中连续的字符序列。
15+
- **好子字符串**:如果一个字符串中不含有任何重复字符,则称这个字符串为好子字符串。
16+
- 1ドル \le s.length \le 100$。
17+
- `s` 只包含小写英文字母。
18+
19+
**示例**:
20+
21+
- 示例 1:
22+
23+
```Python
24+
输入:s = "xyzzaz"
25+
输出:1
26+
解释:总共有 4 个长度为 3 的子字符串:"xyz","yzz","zza""zaz"
27+
唯一的长度为 3 的好子字符串是 "xyz"
28+
```
29+
30+
- 示例 2:
31+
32+
```Python
33+
输入:s = "aababcabc"
34+
输出:4
35+
解释:总共有 7 个长度为 3 的子字符串:"aab","aba","bab","abc","bca","cab""abc"
36+
好子字符串包括 "abc","bca","cab""abc"
37+
```
38+
39+
## 解题思路
40+
41+
### 思路 1:模拟
42+
43+
1. 遍历长度为 3 的子字符串。
44+
2. 判断子字符串中的字符是否有重复。如果没有重复,则答案进行计数。
45+
3. 遍历完输出答案。
46+
47+
### 思路 1:代码
48+
49+
```Python
50+
class Solution:
51+
def countGoodSubstrings(self, s: str) -> int:
52+
ans = 0
53+
for i in range(2, len(s)):
54+
if s[i - 2] != s[i - 1] and s[i - 1] != s[i] and s[i - 2] != s[i]:
55+
ans += 1
56+
return ans
57+
```
58+
59+
### 思路 1:复杂度分析
60+
61+
- **时间复杂度**:$O(n)$。
62+
- **空间复杂度**:$O(1)$。

0 commit comments

Comments
(0)

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