Skip to content

Commit cd1b9e5

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

File tree

6 files changed

+210
-2
lines changed

6 files changed

+210
-2
lines changed

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

+73-1
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,87 @@
6060
<!-- 这里可写当前语言的特殊实现逻辑 -->
6161

6262
```python
63-
63+
class Solution:
64+
def areAlmostEqual(self, s1: str, s2: str) -> bool:
65+
cnt, n = 0, len(s1)
66+
c1 = c2 = None
67+
for i in range(n):
68+
if s1[i] != s2[i]:
69+
cnt += 1
70+
if (cnt == 2 and (s1[i] != c2 or s2[i] != c1)) or cnt > 2:
71+
return False
72+
c1, c2 = s1[i], s2[i]
73+
return cnt == 0 or cnt == 2
6474
```
6575

6676
### **Java**
6777

6878
<!-- 这里可写当前语言的特殊实现逻辑 -->
6979

7080
```java
81+
class Solution {
82+
public boolean areAlmostEqual(String s1, String s2) {
83+
int n = s1.length();
84+
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) {
92+
return false;
93+
}
94+
c1 = t1;
95+
c2 = t2;
96+
}
97+
}
98+
return cnt == 0 || cnt == 2;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
bool areAlmostEqual(string s1, string s2) {
109+
char c1 = 0, c2 = 0;
110+
int n = s1.size();
111+
int cnt = 0;
112+
for (int i = 0; i < n; ++i)
113+
{
114+
if (s1[i] != s2[i])
115+
{
116+
++cnt;
117+
if ((cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2) return false;
118+
c1 = s1[i];
119+
c2 = s2[i];
120+
}
121+
}
122+
return cnt == 0 || cnt == 2;
123+
}
124+
};
125+
```
71126
127+
### **Go**
128+
129+
```go
130+
func areAlmostEqual(s1 string, s2 string) bool {
131+
var c1, c2 byte
132+
cnt, n := 0, len(s1)
133+
for i := 0; i < n; i++ {
134+
if s1[i] != s2[i] {
135+
cnt++
136+
if (cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2 {
137+
return false
138+
}
139+
c1, c2 = s1[i], s2[i]
140+
}
141+
}
142+
return cnt == 0 || cnt == 2
143+
}
72144
```
73145

74146
### **...**

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

+73-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,85 @@
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def areAlmostEqual(self, s1: str, s2: str) -> bool:
61+
cnt, n = 0, len(s1)
62+
c1 = c2 = None
63+
for i in range(n):
64+
if s1[i] != s2[i]:
65+
cnt += 1
66+
if (cnt == 2 and (s1[i] != c2 or s2[i] != c1)) or cnt > 2:
67+
return False
68+
c1, c2 = s1[i], s2[i]
69+
return cnt == 0 or cnt == 2
6070
```
6171

6272
### **Java**
6373

6474
```java
75+
class Solution {
76+
public boolean areAlmostEqual(String s1, String s2) {
77+
int n = s1.length();
78+
int cnt = 0;
79+
char c1 = 0;
80+
char c2 = 0;
81+
for (int i = 0; i < n; ++i) {
82+
char t1 = s1.charAt(i), t2 = s2.charAt(i);
83+
if (t1 != t2) {
84+
++cnt;
85+
if ((cnt == 2 && (c1 != t2 || c2 != t1)) || cnt > 2) {
86+
return false;
87+
}
88+
c1 = t1;
89+
c2 = t2;
90+
}
91+
}
92+
return cnt == 0 || cnt == 2;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
bool areAlmostEqual(string s1, string s2) {
103+
char c1 = 0, c2 = 0;
104+
int n = s1.size();
105+
int cnt = 0;
106+
for (int i = 0; i < n; ++i)
107+
{
108+
if (s1[i] != s2[i])
109+
{
110+
++cnt;
111+
if ((cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2) return false;
112+
c1 = s1[i];
113+
c2 = s2[i];
114+
}
115+
}
116+
return cnt == 0 || cnt == 2;
117+
}
118+
};
119+
```
65120
121+
### **Go**
122+
123+
```go
124+
func areAlmostEqual(s1 string, s2 string) bool {
125+
var c1, c2 byte
126+
cnt, n := 0, len(s1)
127+
for i := 0; i < n; i++ {
128+
if s1[i] != s2[i] {
129+
cnt++
130+
if (cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2 {
131+
return false
132+
}
133+
c1, c2 = s1[i], s2[i]
134+
}
135+
}
136+
return cnt == 0 || cnt == 2
137+
}
66138
```
67139

68140
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool areAlmostEqual(string s1, string s2) {
4+
char c1 = 0, c2 = 0;
5+
int n = s1.size();
6+
int cnt = 0;
7+
for (int i = 0; i < n; ++i)
8+
{
9+
if (s1[i] != s2[i])
10+
{
11+
++cnt;
12+
if ((cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2) return false;
13+
c1 = s1[i];
14+
c2 = s2[i];
15+
}
16+
}
17+
return cnt == 0 || cnt == 2;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func areAlmostEqual(s1 string, s2 string) bool {
2+
var c1, c2 byte
3+
cnt, n := 0, len(s1)
4+
for i := 0; i < n; i++ {
5+
if s1[i] != s2[i] {
6+
cnt++
7+
if (cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2 {
8+
return false
9+
}
10+
c1, c2 = s1[i], s2[i]
11+
}
12+
}
13+
return cnt == 0 || cnt == 2
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean areAlmostEqual(String s1, String s2) {
3+
int n = s1.length();
4+
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) {
12+
return false;
13+
}
14+
c1 = t1;
15+
c2 = t2;
16+
}
17+
}
18+
return cnt == 0 || cnt == 2;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def areAlmostEqual(self, s1: str, s2: str) -> bool:
3+
cnt, n = 0, len(s1)
4+
c1 = c2 = None
5+
for i in range(n):
6+
if s1[i] != s2[i]:
7+
cnt += 1
8+
if (cnt == 2 and (s1[i] != c2 or s2[i] != c1)) or cnt > 2:
9+
return False
10+
c1, c2 = s1[i], s2[i]
11+
return cnt == 0 or cnt == 2

0 commit comments

Comments
 (0)