Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 930cef4

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

File tree

3 files changed

+180
-81
lines changed

3 files changed

+180
-81
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
3216. Lexicographically Smallest String After a Swap
3+
https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap/description/
4+
5+
Problem:
6+
Given a string s containing only digits, return the lexicographically smallest string
7+
that can be obtained after swapping adjacent digits in s with the same parity at most once.
8+
9+
Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4,
10+
have the same parity, while 6 and 9 do not.
11+
12+
Example 1:
13+
14+
Input: s = "45320"
15+
16+
Output: "43520"
17+
18+
Explanation:
19+
20+
s[1] == '5' and s[2] == '3' both have the same parity, and swapping them results in the lexicographically smallest string.
21+
22+
Example 2:
23+
24+
Input: s = "001"
25+
26+
Output: "001"
27+
28+
Explanation:
29+
30+
There is no need to perform a swap because s is already the lexicographically smallest.
31+
32+
33+
Constraints:
34+
35+
2 <= s.length <= 100
36+
s consists only of digits.
37+
*/
38+
39+
/*
40+
Approach:
41+
42+
Checking if the present digit is greater than the next digit, if yes then checking the parity of the digit.
43+
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.
44+
return the updated number
45+
46+
What is parity of a number?
47+
Parity: the property of an integer of whether it is even or odd
48+
49+
how to check the parity of the number:
50+
- using the & operation on the last bit,
51+
- if (last bit)&1 == 1, means the last bit was 1. means Odd (ex: 3 has a bit representation of 11)
52+
- if (last bit)&1 == 0, means last bit was 0. means Even number ( ex: 2 has a bit representation of 10)
53+
54+
*/
55+
56+
57+
/**
58+
* @param {string} s
59+
* @return {string}
60+
*/
61+
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+
}
76+
}
77+
78+
return arr.join('');
79+
};
80+
81+
module.exports.getSmallestString = getSmallestString;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const assert = require("assert");
2+
const bitReverseToMakeNumberEqual = require("../../../LeetcodeProblems/Algorithms/easy/Lexographic_smallest_after_swap").getSmallestString;
3+
4+
5+
function test() {
6+
assert.deepEqual(
7+
getSmallestString("45320"),
8+
"43520"
9+
);
10+
assert.deepEqual(
11+
getSmallestString("001"),
12+
"001"
13+
);
14+
);
15+
}
16+
17+
module.exports.test = test;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /