Skip to content

Latest commit

 

History

History

lexicographically-smallest-string-after-a-swap

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Lexicographically smallest string after a swap

Problem link

Solutions

Solution.py

# https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap/

class Solution:
    def getSmallestString(self, s: str) -> str:
        n = len(s)
        for i in range(1, n):
            x, y = s[i-1:i+1]
            if y < x and int(x) % 2 == int(y) % 2:
                return s[:i-1] + y + x + s[i+1:]
        return s

Tags