forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] main from itcharge:main #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 1876. 长度为三且各字符不同的子字符串.md
- Loading branch information
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
Solutions/1876. 长度为三且各字符不同的子字符串.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# [1876. 长度为三且各字符不同的子字符串](https://leetcode.cn/problems/substrings-of-size-three-with-distinct-characters/description/) | ||
|
||
- 标签:哈希表、字符串、计数、滑动窗口 | ||
- 难度:简单 | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定搞一个字符串 `s`。 | ||
|
||
**要求**:返回 `s` 中长度为 3ドル$ 的好子字符串的数量。如果相同的好子字符串出现多次,则每一次都应该被记入答案之中。 | ||
|
||
**说明**: | ||
|
||
- **子字符串**:指的是一个字符串中连续的字符序列。 | ||
- **好子字符串**:如果一个字符串中不含有任何重复字符,则称这个字符串为好子字符串。 | ||
- 1ドル \le s.length \le 100$。 | ||
- `s` 只包含小写英文字母。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```Python | ||
输入:s = "xyzzaz" | ||
输出:1 | ||
解释:总共有 4 个长度为 3 的子字符串:"xyz","yzz","zza" 和 "zaz" 。 | ||
唯一的长度为 3 的好子字符串是 "xyz" 。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```Python | ||
输入:s = "aababcabc" | ||
输出:4 | ||
解释:总共有 7 个长度为 3 的子字符串:"aab","aba","bab","abc","bca","cab" 和 "abc" 。 | ||
好子字符串包括 "abc","bca","cab" 和 "abc" 。 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:模拟 | ||
|
||
1. 遍历长度为 3 的子字符串。 | ||
2. 判断子字符串中的字符是否有重复。如果没有重复,则答案进行计数。 | ||
3. 遍历完输出答案。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def countGoodSubstrings(self, s: str) -> int: | ||
ans = 0 | ||
for i in range(2, len(s)): | ||
if s[i - 2] != s[i - 1] and s[i - 1] != s[i] and s[i - 2] != s[i]: | ||
ans += 1 | ||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n)$。 | ||
- **空间复杂度**:$O(1)$。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.