1625. Lexicographically Smallest String After Applying Operations

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1625

class Solution:
    def findLexSmallestString(self, s: str, a: int, b: int) -> str:
        res = s
        n = len(s)
        s = list(s)
        for _ in range(n):
            s = s[-b:] + s[:-b]
            for j in range(10):
                for k in range(1, n, 2):
                    s[k] = str((int(s[k]) + a) % 10)
                if b & 1:
                    for p in range(10):
                        for k in range(0, n, 2):
                            s[k] = str((int(s[k]) + a) % 10)
                        t = ''.join(s)
                        if res > t:
                            res = t
                else:
                    t = ''.join(s)
                    if res > t:
                        res = t
        return res