Skip to content

Commit abcc85f

Browse files
author
mukul
committed
Lexographically smallest after a swap
1 parent 930cef4 commit abcc85f

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

LeetcodeProblems/Algorithms/easy/Lexographic_smallest_after_swap.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@ how to check the parity of the number:
5959
* @return {string}
6060
*/
6161
var getSmallestString = function(s) {
62-
let arr = s.split('').map(Number);
63-
64-
const getParity = (num) => {
65-
if(num&1 === 0) return 'even';
66-
else return 'odd';
67-
}
68-
69-
for(let i = 0; i< s.length - 1; i++) {
70-
if(arr[i] > arr[i+1] && getParity(arr[i]) === getParity(arr[i + 1])) {
71-
let tmp = arr[i+1];
72-
arr[i+1] = arr[i];
73-
arr[i] = tmp;
74-
break;
75-
}
62+
let arr = s.split('').map(Number);
63+
64+
const getParity = (num) => {
65+
if(num&1 === 0) return 'even';
66+
else return 'odd';
67+
}
68+
69+
for(let i = 0; i< s.length - 1; i++) {
70+
if(arr[i] > arr[i+1] && getParity(arr[i]) === getParity(arr[i + 1])) {
71+
let tmp = arr[i+1];
72+
arr[i+1] = arr[i];
73+
arr[i] = tmp;
74+
break;
7675
}
76+
}
7777

78-
return arr.join('');
78+
return arr.join('');
7979
};
8080

8181
module.exports.getSmallestString = getSmallestString;

0 commit comments

Comments
 (0)