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 9bfcd02

Browse files
✨feat: add 1790
1 parent 724099b commit 9bfcd02

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-atnd/) | 简单 | 🤩🤩🤩🤩 |
195195
| [1763. 最长的美好子字符串](https://leetcode-cn.com/problems/longest-nice-substring/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-nice-substring/solution/cong-shu-ju-fan-wei-xuan-ze-he-gua-suan-n3y2a/) | 简单 | 🤩🤩🤩 |
196196
| [1784. 检查二进制字符串字段](https://leetcode.cn/problems/check-if-binary-string-has-at-most-one-segment-of-ones/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-binary-string-has-at-most-one-segment-of-ones/solution/by-ac_oier-kiu6/) | 简单 | 🤩🤩🤩 |
197+
| [1790. 仅执行一次字符串交换能否使两个字符串相等](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/solution/by-ac_oier-qeul/) | 简单 | 🤩🤩🤩🤩🤩 |
197198
| [1791. 找出星型图的中心节点](https://leetcode-cn.com/problems/find-center-of-star-graph/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-center-of-star-graph/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-qoix/) | 简单 | 🤩🤩🤩 |
198199
| [1816. 截断句子](https://leetcode-cn.com/problems/truncate-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/truncate-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-l7gu/) | 简单 | 🤩🤩🤩🤩 |
199200
| [1823. 找出游戏的获胜者](https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game/solution/by-ac_oier-qsuq/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1790. 仅执行一次字符串交换能否使两个字符串相等](https://leetcode-cn.com/problems/make-the-xor-of-all-segments-equal-to-zero/solution/gong-shui-san-xie-chou-xiang-cheng-er-we-ww79/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你长度相等的两个字符串 `s1``s2` 。一次 字符串交换 操作的步骤如下:选出某个字符串中的两个下标(不必不同),并交换这两个下标所对应的字符。
10+
11+
如果对 其中一个字符串 执行 最多一次字符串交换 就可以使两个字符串相等,返回 `true` ;否则,返回 `false`
12+
13+
示例 1:
14+
```
15+
输入:s1 = "bank", s2 = "kanb"
16+
17+
输出:true
18+
19+
解释:例如,交换 s2 中的第一个和最后一个字符可以得到 "bank"
20+
```
21+
示例 2:
22+
```
23+
输入:s1 = "attack", s2 = "defend"
24+
25+
输出:false
26+
27+
解释:一次字符串交换无法使两个字符串相等
28+
```
29+
示例 3:
30+
```
31+
输入:s1 = "kelb", s2 = "kelb"
32+
33+
输出:true
34+
35+
解释:两个字符串已经相等,所以不需要进行字符串交换
36+
```
37+
示例 4:
38+
```
39+
输入:s1 = "abcd", s2 = "dcba"
40+
41+
输出:false
42+
```
43+
44+
提示:
45+
* 1ドル <= s1.length, s2.length <= 100$
46+
* $s1.length = s2.length$
47+
* `s1``s2` 仅由小写英文字母组成
48+
49+
---
50+
51+
### 模拟
52+
53+
根据题意进行模拟即可 : 使用 `a``b` 记录不同的位置下标,初始值为 `-1`,若「不同位置超过 2ドル$ 个」或「只有 1ドル$ 个」直接返回 `false`,若「不存在不同位置」或「不同位置字符相同」,则返回 `true`
54+
55+
Java 代码:
56+
```Java
57+
class Solution {
58+
public boolean areAlmostEqual(String s1, String s2) {
59+
int n = s1.length(), a = -1, b = -1;
60+
for (int i = 0; i < n; i++) {
61+
if (s1.charAt(i) == s2.charAt(i)) continue;
62+
if (a == -1) a = i;
63+
else if (b == -1) b = i;
64+
else return false;
65+
}
66+
if (a == b && b == -1) return true;
67+
if (a == -1 || b == -1) return false;
68+
return s1.charAt(a) == s2.charAt(b) && s1.charAt(b) == s2.charAt(a);
69+
}
70+
}
71+
```
72+
TypeScript 代码:
73+
```TypeScript
74+
function areAlmostEqual(s1: string, s2: string): boolean {
75+
let n = s1.length, a = -1, b = -1
76+
for (let i = 0; i < n; i++) {
77+
if (s1[i] == s2[i]) continue
78+
if (a == -1) a = i
79+
else if (b == -1) b = i
80+
else return false
81+
}
82+
if (a == -1 && b == -1) return true
83+
if (a == -1 || b == -1) return false
84+
return s1[a] == s2[b] && s1[b] == s2[a]
85+
}
86+
```
87+
Python 代码:
88+
```Python
89+
class Solution:
90+
def areAlmostEqual(self, s1: str, s2: str) -> bool:
91+
n, a, b = len(s1), -1, -1
92+
for i in range(n):
93+
if s1[i] == s2[i]:
94+
continue
95+
if a == -1:
96+
a = i
97+
elif b == -1:
98+
b = i
99+
else:
100+
return False
101+
if a == b == -1:
102+
return True
103+
if a == -1 or b == -1:
104+
return False
105+
return s1[a] == s2[b] and s1[b] == s2[a]
106+
```
107+
* 时间复杂度:$O(n)$
108+
* 空间复杂度:$O(1)$
109+
110+
---
111+
112+
### 最后
113+
114+
这是我们「刷穿 LeetCode」系列文章的第 `No.1790` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
115+
116+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
117+
118+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
119+
120+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
121+

0 commit comments

Comments
(0)

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