- Notifications
You must be signed in to change notification settings - Fork98
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
ignacio-chiazzo merged 3 commits intoignacio-chiazzo:masterfrommeetmukul1993:leetcode_3216Oct 19, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletionsLeetcodeProblems/Algorithms/easy/Lexographic_smallest_after_swap.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
3216. Lexicographically Smallest String After a Swap | ||
https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap/description/ | ||
Problem: | ||
Given a string s containing only digits, return the lexicographically smallest string | ||
that can be obtained after swapping adjacent digits in s with the same parity at most once. | ||
Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, | ||
have the same parity, while 6 and 9 do not. | ||
Example 1: | ||
Input: s = "45320" | ||
Output: "43520" | ||
Explanation: | ||
s[1] == '5' and s[2] == '3' both have the same parity, and swapping them results in the lexicographically smallest string. | ||
Example 2: | ||
Input: s = "001" | ||
Output: "001" | ||
Explanation: | ||
There is no need to perform a swap because s is already the lexicographically smallest. | ||
Constraints: | ||
2 <= s.length <= 100 | ||
s consists only of digits. | ||
*/ | ||
/* | ||
Approach: | ||
Checking if the present digit is greater than the next digit, if yes then checking the parity of the digit. | ||
if both have the same parity swap the number. since this operation can be done at max once, break the loop after the first swap. | ||
return the updated number | ||
What is parity of a number? | ||
Parity: the property of an integer of whether it is even or odd | ||
how to check the parity of the number: | ||
- using the & operation on the last bit, | ||
- if (last bit)&1 == 1, means the last bit was 1. means Odd (ex: 3 has a bit representation of 11) | ||
- if (last bit)&1 == 0, means last bit was 0. means Even number ( ex: 2 has a bit representation of 10) | ||
*/ | ||
/** | ||
* @param {string} s | ||
* @return {string} | ||
*/ | ||
var getSmallestString = function(s) { | ||
let arr = s.split("").map(Number); | ||
const getParity = (num) => { | ||
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])) { | ||
let tmp = arr[i+1]; | ||
arr[i+1] = arr[i]; | ||
arr[i] = tmp; | ||
break; | ||
} | ||
} | ||
return arr.join(""); | ||
}; | ||
module.exports.getSmallestString = getSmallestString; |
17 changes: 17 additions & 0 deletionsLeetcodeProblemsTests/Algorithms/easy/Lexographic_smallest_after_swap.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const assert = require("assert"); | ||
const bitReverseToMakeNumberEqual = require("../../../LeetcodeProblems/Algorithms/easy/Lexographic_smallest_after_swap").getSmallestString; | ||
function test() { | ||
assert.deepEqual( | ||
getSmallestString("45320"), | ||
"43520" | ||
); | ||
assert.deepEqual( | ||
getSmallestString("001"), | ||
"001" | ||
); | ||
); | ||
} | ||
module.exports.test = test; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.