Skip to content

Commit 3153d62

Browse files
committed
feat: add solutions to lc problem: No.1790
No.1790.Check if One String Swap Can Make Strings Equal
1 parent 17b3b50 commit 3153d62

File tree

7 files changed

+158
-96
lines changed

7 files changed

+158
-96
lines changed

solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README.md

+61-32
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:简单计数**
57+
58+
我们用变量 $cnt$ 记录两个字符串中相同位置字符不同的个数,两个字符串若满足题目要求,那么 $cnt$ 一定为 $0$ 或 $2$。另外用两个字符变量 $c1$ 和 $c2$ 记录两个字符串中相同位置字符不同的字符。
59+
60+
同时遍历两个字符串,对于相同位置的两个字符 $a$ 和 $b$,如果 $a \ne b$,那么 $cnt$ 自增 $1$。如果此时 $cnt$ 大于 $2$,或者 $cnt$ 为 $2$ 且 $a \ne c2$ 或 $b \ne c1$,那么直接返回 `false`。注意记录一下 $c1$ 和 $c2$。
61+
62+
遍历结束,若 $cnt\neq 1$,返回 `true`
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**
@@ -62,15 +72,15 @@
6272
```python
6373
class Solution:
6474
def areAlmostEqual(self, s1: str, s2: str) -> bool:
65-
cnt, n = 0, len(s1)
75+
cnt = 0
6676
c1 = c2 = None
67-
for i in range(n):
68-
if s1[i] != s2[i]:
77+
for a, b in zip(s1, s2):
78+
if a != b:
6979
cnt += 1
70-
if (cnt == 2 and (s1[i] != c2 or s2[i] != c1)) or cnt > 2:
80+
if cnt > 2 or (cnt == 2 and (a != c2 or b != c1)):
7181
return False
72-
c1, c2 = s1[i], s2[i]
73-
return cnt == 0 or cnt == 2
82+
c1, c2 = a, b
83+
return cnt != 1
7484
```
7585

7686
### **Java**
@@ -80,22 +90,19 @@ class Solution:
8090
```java
8191
class Solution {
8292
public boolean areAlmostEqual(String s1, String s2) {
83-
int n = s1.length();
8493
int cnt = 0;
85-
char c1 = 0;
86-
char c2 = 0;
87-
for (int i = 0; i < n; ++i) {
88-
char t1 = s1.charAt(i), t2 = s2.charAt(i);
89-
if (t1 != t2) {
90-
++cnt;
91-
if ((cnt == 2 && (c1 != t2 || c2 != t1)) || cnt > 2) {
94+
char c1 = 0, c2 = 0;
95+
for (int i = 0; i < s1.length(); ++i) {
96+
char a = s1.charAt(i), b = s2.charAt(i);
97+
if (a != b) {
98+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
9299
return false;
93100
}
94-
c1 = t1;
95-
c2 = t2;
101+
c1 = a;
102+
c2 = b;
96103
}
97104
}
98-
return cnt == 0 || cnt == 2;
105+
return cnt != 1;
99106
}
100107
}
101108
```
@@ -106,18 +113,18 @@ class Solution {
106113
class Solution {
107114
public:
108115
bool areAlmostEqual(string s1, string s2) {
109-
char c1 = 0, c2 = 0;
110-
int n = s1.size();
111116
int cnt = 0;
112-
for (int i = 0; i < n; ++i) {
113-
if (s1[i] != s2[i]) {
114-
++cnt;
115-
if ((cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2) return false;
116-
c1 = s1[i];
117-
c2 = s2[i];
117+
char c1 = 0, c2 = 0;
118+
for (int i = 0; i < s1.size(); ++i) {
119+
char a = s1[i], b = s2[i];
120+
if (a != b) {
121+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
122+
return false;
123+
}
124+
c1 = a, c2 = b;
118125
}
119126
}
120-
return cnt == 0 || cnt == 2;
127+
return cnt != 1;
121128
}
122129
};
123130
```
@@ -126,18 +133,40 @@ public:
126133
127134
```go
128135
func areAlmostEqual(s1 string, s2 string) bool {
136+
cnt := 0
129137
var c1, c2 byte
130-
cnt, n := 0, len(s1)
131-
for i := 0; i < n; i++ {
132-
if s1[i] != s2[i] {
138+
for i := range s1 {
139+
a, b := s1[i], s2[i]
140+
if a != b {
133141
cnt++
134-
if (cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2 {
142+
if cnt > 2 || (cnt == 2 && (a != c2 || b != c1)) {
135143
return false
136144
}
137-
c1, c2 = s1[i], s2[i]
145+
c1, c2 = a, b
138146
}
139147
}
140-
return cnt == 0 || cnt == 2
148+
return cnt != 1
149+
}
150+
```
151+
152+
### **TypeScript**
153+
154+
```ts
155+
function areAlmostEqual(s1: string, s2: string): boolean {
156+
let c1, c2;
157+
let cnt = 0;
158+
for (let i = 0; i < s1.length; ++i) {
159+
const a = s1.charAt(i);
160+
const b = s2.charAt(i);
161+
if (a != b) {
162+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
163+
return false;
164+
}
165+
c1 = a;
166+
c2 = b;
167+
}
168+
}
169+
return cnt != 1;
141170
}
142171
```
143172

solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README_EN.md

+51-32
Original file line numberDiff line numberDiff line change
@@ -51,38 +51,35 @@
5151
```python
5252
class Solution:
5353
def areAlmostEqual(self, s1: str, s2: str) -> bool:
54-
cnt, n = 0, len(s1)
54+
cnt = 0
5555
c1 = c2 = None
56-
for i in range(n):
57-
if s1[i] != s2[i]:
56+
for a, b in zip(s1, s2):
57+
if a != b:
5858
cnt += 1
59-
if (cnt == 2 and (s1[i] != c2 or s2[i] != c1)) or cnt > 2:
59+
if cnt > 2 or (cnt == 2 and (a != c2 or b != c1)):
6060
return False
61-
c1, c2 = s1[i], s2[i]
62-
return cnt == 0 or cnt == 2
61+
c1, c2 = a, b
62+
return cnt != 1
6363
```
6464

6565
### **Java**
6666

6767
```java
6868
class Solution {
6969
public boolean areAlmostEqual(String s1, String s2) {
70-
int n = s1.length();
7170
int cnt = 0;
72-
char c1 = 0;
73-
char c2 = 0;
74-
for (int i = 0; i < n; ++i) {
75-
char t1 = s1.charAt(i), t2 = s2.charAt(i);
76-
if (t1 != t2) {
77-
++cnt;
78-
if ((cnt == 2 && (c1 != t2 || c2 != t1)) || cnt > 2) {
71+
char c1 = 0, c2 = 0;
72+
for (int i = 0; i < s1.length(); ++i) {
73+
char a = s1.charAt(i), b = s2.charAt(i);
74+
if (a != b) {
75+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
7976
return false;
8077
}
81-
c1 = t1;
82-
c2 = t2;
78+
c1 = a;
79+
c2 = b;
8380
}
8481
}
85-
return cnt == 0 || cnt == 2;
82+
return cnt != 1;
8683
}
8784
}
8885
```
@@ -93,18 +90,18 @@ class Solution {
9390
class Solution {
9491
public:
9592
bool areAlmostEqual(string s1, string s2) {
96-
char c1 = 0, c2 = 0;
97-
int n = s1.size();
9893
int cnt = 0;
99-
for (int i = 0; i < n; ++i) {
100-
if (s1[i] != s2[i]) {
101-
++cnt;
102-
if ((cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2) return false;
103-
c1 = s1[i];
104-
c2 = s2[i];
94+
char c1 = 0, c2 = 0;
95+
for (int i = 0; i < s1.size(); ++i) {
96+
char a = s1[i], b = s2[i];
97+
if (a != b) {
98+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
99+
return false;
100+
}
101+
c1 = a, c2 = b;
105102
}
106103
}
107-
return cnt == 0 || cnt == 2;
104+
return cnt != 1;
108105
}
109106
};
110107
```
@@ -113,18 +110,40 @@ public:
113110
114111
```go
115112
func areAlmostEqual(s1 string, s2 string) bool {
113+
cnt := 0
116114
var c1, c2 byte
117-
cnt, n := 0, len(s1)
118-
for i := 0; i < n; i++ {
119-
if s1[i] != s2[i] {
115+
for i := range s1 {
116+
a, b := s1[i], s2[i]
117+
if a != b {
120118
cnt++
121-
if (cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2 {
119+
if cnt > 2 || (cnt == 2 && (a != c2 || b != c1)) {
122120
return false
123121
}
124-
c1, c2 = s1[i], s2[i]
122+
c1, c2 = a, b
125123
}
126124
}
127-
return cnt == 0 || cnt == 2
125+
return cnt != 1
126+
}
127+
```
128+
129+
### **TypeScript**
130+
131+
```ts
132+
function areAlmostEqual(s1: string, s2: string): boolean {
133+
let c1, c2;
134+
let cnt = 0;
135+
for (let i = 0; i < s1.length; ++i) {
136+
const a = s1.charAt(i);
137+
const b = s2.charAt(i);
138+
if (a != b) {
139+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
140+
return false;
141+
}
142+
c1 = a;
143+
c2 = b;
144+
}
145+
}
146+
return cnt != 1;
128147
}
129148
```
130149

Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class Solution {
22
public:
33
bool areAlmostEqual(string s1, string s2) {
4-
char c1 = 0, c2 = 0;
5-
int n = s1.size();
64
int cnt = 0;
7-
for (int i = 0; i < n; ++i) {
8-
if (s1[i] != s2[i]) {
9-
++cnt;
10-
if ((cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2) return false;
11-
c1 = s1[i];
12-
c2 = s2[i];
5+
char c1 = 0, c2 = 0;
6+
for (int i = 0; i < s1.size(); ++i) {
7+
char a = s1[i], b = s2[i];
8+
if (a != b) {
9+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
10+
return false;
11+
}
12+
c1 = a, c2 = b;
1313
}
1414
}
15-
return cnt == 0 || cnt == 2;
15+
return cnt != 1;
1616
}
1717
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
func areAlmostEqual(s1 string, s2 string) bool {
2+
cnt := 0
23
var c1, c2 byte
3-
cnt, n := 0, len(s1)
4-
for i := 0; i < n; i++ {
5-
if s1[i] != s2[i] {
4+
for i := range s1 {
5+
a, b := s1[i], s2[i]
6+
if a != b {
67
cnt++
7-
if (cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2 {
8+
if cnt > 2 || (cnt == 2 && (a != c2 || b != c1)) {
89
return false
910
}
10-
c1, c2 = s1[i], s2[i]
11+
c1, c2 = a, b
1112
}
1213
}
13-
return cnt == 0 || cnt == 2
14+
return cnt != 1
1415
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
class Solution {
22
public boolean areAlmostEqual(String s1, String s2) {
3-
int n = s1.length();
43
int cnt = 0;
5-
char c1 = 0;
6-
char c2 = 0;
7-
for (int i = 0; i < n; ++i) {
8-
char t1 = s1.charAt(i), t2 = s2.charAt(i);
9-
if (t1 != t2) {
10-
++cnt;
11-
if ((cnt == 2 && (c1 != t2 || c2 != t1)) || cnt > 2) {
4+
char c1 = 0, c2 = 0;
5+
for (int i = 0; i < s1.length(); ++i) {
6+
char a = s1.charAt(i), b = s2.charAt(i);
7+
if (a != b) {
8+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
129
return false;
1310
}
14-
c1 = t1;
15-
c2 = t2;
11+
c1 = a;
12+
c2 = b;
1613
}
1714
}
18-
return cnt == 0 || cnt == 2;
15+
return cnt != 1;
1916
}
2017
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def areAlmostEqual(self, s1: str, s2: str) -> bool:
3-
cnt, n = 0, len(s1)
3+
cnt = 0
44
c1 = c2 = None
5-
for i in range(n):
6-
if s1[i] != s2[i]:
5+
for a, b in zip(s1, s2):
6+
if a != b:
77
cnt += 1
8-
if (cnt == 2 and (s1[i] != c2 or s2[i] != c1)) or cnt > 2:
8+
if cnt > 2 or (cnt == 2 and (a != c2 or b != c1)):
99
return False
10-
c1, c2 = s1[i], s2[i]
11-
return cnt == 0 or cnt == 2
10+
c1, c2 = a, b
11+
return cnt != 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function areAlmostEqual(s1: string, s2: string): boolean {
2+
let c1, c2;
3+
let cnt = 0;
4+
for (let i = 0; i < s1.length; ++i) {
5+
const a = s1.charAt(i);
6+
const b = s2.charAt(i);
7+
if (a != b) {
8+
if (++cnt > 2 || (cnt == 2 && (a != c2 || b != c1))) {
9+
return false;
10+
}
11+
c1 = a;
12+
c2 = b;
13+
}
14+
}
15+
return cnt != 1;
16+
}

0 commit comments

Comments
 (0)