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
/
string
/
swap_adjacent_in_lr_string.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
58 lines (54 loc) · 1.73 KB
main
Breadcrumbs
cpp-practice
/
src
/
string
/
swap_adjacent_in_lr_string.cpp
Copy path
Top
File metadata and controls
Code
Blame
58 lines (54 loc) · 1.73 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 在LR字符串中交换相邻字符
// https://leetcode.cn/problems/swap-adjacent-in-lr-string
// INLINE ../../images/string/swap_adjacent_in_lr_string.jpeg
// 解题思路:不需要真的去交换字符,只需要判断相对顺序是否一致且满足交换条件。
#include <headers.hpp>
class Solution {
public:
bool canTransform(string start, string end) {
int len = end.length();
int i = 0, j = 0;
while (i < len && j < len) {
while (i < len && start[i] == 'X') { // 找到start中第一个不是'X'的字符
i++;
}
while (j < len && end[j] == 'X') { // 找到end中第一个不是'X'的字符
j++;
}
if (i < len && j < len) {
if (start[i] !=
end[j]) { // 如果两个字符不相同,则无法通过交换相邻字符得到end
return false;
}
// 判断是否满足交换条件
// XXL
// XLX
// XXR
// XRX
// if (i >= j && start[i] == 'L' || (i <= j && start[i] == 'R')) {
if ((i < j && start[i] == 'L') || (i > j && start[i] == 'R')) {
return false;
}
i++; // 继续比较下一个字符
j++;
}
}
// 没有下面的判断会导致只有一个字符的时候错误 例如:"X","L"
while (
i <
len) { // 如果start中还剩下不是'X'的字符,则无法通过交换相邻字符得到end
if (start[i] != 'X') {
return false;
}
i++;
}
while (j <
len) { // 如果end中还剩下不是'X'的字符,则无法通过交换相邻字符得到end
if (end[j] != 'X') {
return false;
}
j++;
}
return true; // 成功通过交换相邻字符得到end
}
};
You can’t perform that action at this time.