Skip to content

Practice Lexicographically Smallest String After a Swap

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#3216
LeetCode Problem

Lexicographically Smallest String After a Swap

Problem Statement

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.

Source: Lexicographically Smallest String After a Swap on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "45320"
Output
"43520"

# Example 2

Input
s = "001"
Output
"001"

Constraints

2 <= s.length <= 100
s consists only of digits.

Related LeetCode Problems

Practice more problems similar to Lexicographically Smallest String After a Swap.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.