File tree Expand file tree Collapse file tree 3 files changed +56
-1
lines changed
solution/1200-1299/1247.Minimum Swaps to Make Strings Equal Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,26 @@ func minimumSwap(s1 string, s2 string) int {
147
147
return xy/2 + yx/2 + xy%2 + yx%2
148
148
}
149
149
```
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
+ ```
150
170
151
171
### ** ...**
152
172
Original file line number Diff line number Diff line change @@ -122,7 +122,26 @@ func minimumSwap(s1 string, s2 string) int {
122
122
return xy/2 + yx/2 + xy%2 + yx%2
123
123
}
124
124
```
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
+ ```
126
145
### ** ...**
127
146
128
147
```
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments