Skip to content

Lexographically smallest after a swap #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Lexographically smallest after a swap
  • Loading branch information
mukul committed Oct 19, 2024
commit 2ef3870da296d2c787fb274db5828d62e0c47d6b
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ how to check the parity of the number:
* @return {string}
*/
var getSmallestString = function(s) {
let arr = s.split('').map(Number);
let arr = s.split("").map(Number);

const getParity = (num) => {
if(num&1 === 0) return 'even';
else return 'odd';
}
if(num&1 === 0) return "even";
else return "odd";
};

for(let i = 0; i< s.length - 1; i++) {
if(arr[i] > arr[i+1] && getParity(arr[i]) === getParity(arr[i + 1])) {
Expand All @@ -75,7 +75,7 @@ var getSmallestString = function(s) {
}
}

return arr.join('');
return arr.join("");
};

module.exports.getSmallestString = getSmallestString;
Loading