3484. Lexicographically Smallest String After a Swap
My accepted Python solution to LeetCode problem 3484, Lexicographically Smallest String After a Swap, running in 0ms.
- Difficulty: Easy
- Python
- Runtime 0ms
- Memory 17.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.2MB, accepted 2026-01-01.
class Solution:
def getSmallestString(self, s: str) -> str:
s = list(s)
for i in range(len(s) - 1):
a, b = int(s[i]), int(s[i + 1])
# Same parity and swap makes it smaller
if a % 2 == b % 2 and a > b:
s[i], s[i + 1] = s[i + 1], s[i]
break
return ''.join(s)