Skip to content

Commit 649c994

Browse files
authored
feat: add solutions to lc problem: No.1247 (doocs#897)
1 parent e69e9bb commit 649c994

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ func minimumSwap(s1 string, s2 string) int {
147147
return xy/2 + yx/2 + xy%2 + yx%2
148148
}
149149
```
150+
### **JavaScript**
151+
152+
```js
153+
var minimumSwap = function (s1, s2) {
154+
let xy = 0, yx = 0;
155+
for (let i = 0; i < s1.length; ++i) {
156+
const a = s1[i], b = s2[i];
157+
if (a < b) {
158+
++xy;
159+
}
160+
if (a > b) {
161+
++yx;
162+
}
163+
}
164+
if ((xy + yx) % 2 === 1) {
165+
return -1;
166+
}
167+
return Math.floor(xy / 2) + Math.floor(yx / 2) + xy % 2 + yx % 2;
168+
};
169+
```
150170

151171
### **...**
152172

solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README_EN.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,26 @@ func minimumSwap(s1 string, s2 string) int {
122122
return xy/2 + yx/2 + xy%2 + yx%2
123123
}
124124
```
125-
125+
### **JavaScript**
126+
127+
```js
128+
var minimumSwap = function (s1, s2) {
129+
let xy = 0, yx = 0;
130+
for (let i = 0; i < s1.length; ++i) {
131+
const a = s1[i], b = s2[i];
132+
if (a < b) {
133+
++xy;
134+
}
135+
if (a > b) {
136+
++yx;
137+
}
138+
}
139+
if ((xy + yx) % 2 === 1) {
140+
return -1;
141+
}
142+
return Math.floor(xy / 2) + Math.floor(yx / 2) + xy % 2 + yx % 2;
143+
};
144+
```
126145
### **...**
127146

128147
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var minimumSwap = function (s1, s2) {
2+
let xy = 0, yx = 0;
3+
for (let i = 0; i < s1.length; ++i) {
4+
const a = s1[i], b = s2[i];
5+
if (a < b) {
6+
++xy;
7+
}
8+
if (a > b) {
9+
++yx;
10+
}
11+
}
12+
if ((xy + yx) % 2 === 1) {
13+
return -1;
14+
}
15+
return Math.floor(xy / 2) + Math.floor(yx / 2) + xy % 2 + yx % 2;
16+
};

0 commit comments

Comments
 (0)