Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
yi-ge
cpp-practice
Repository navigation
Code
Issues
Pull requests
1
(1)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cpp-practice
/
src
/
array
/
check_distances_between_same_letters.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
29 lines (28 loc) · 1.03 KB
main
Breadcrumbs
cpp-practice
/
src
/
array
/
check_distances_between_same_letters.cpp
Copy path
Top
File metadata and controls
Code
Blame
29 lines (28 loc) · 1.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 检查相同字母间的距离
// https://leetcode.cn/problems/check-distances-between-same-letters
// INLINE ../../images/array/check_distances_between_same_letters.jpeg
#include <headers.hpp>
class Solution {
public:
// distance 是一个整数数组,其中的元素表示每个字母之间的最大距离。
// 如果字符串 s 满足以下条件,则返回 true:
// 1. s 的长度为 n,distance 的长度为 m;
// 2. s[i] == s[j] 且 |i - j| < distance[s[i] - 'a'] 对于所有的 0 <= i < j < n
// 都成立。
bool checkDistances(string s, vector<int> &distance) {
int n = s.size();
int m = distance.size();
// last[i] 表示字符 i 最后出现的位置。
vector<int> last(m, -1);
// 遍历字符串 s,更新 last 数组
for (int i = 0; i < n; ++i) {
int x = s[i] - 'a';
// 如果字符 x 已经出现过,检查距离是否符合要求
if (last[x] != -1 && i - last[x] - 1 != distance[x]) {
return false;
}
last[x] = i;
}
return true;
}
};
You can’t perform that action at this time.