Problem solution · Python

Lexicographically Smallest String After Applying Operations

Lexicographically Smallest String After Applying Operations: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
74 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Lexicographically Smallest String After Applying Operations, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 74 lines of Python from the credited upstream file lexicographically-smallest-string-after-applying-operations.py.
  • The implementation visibly relies on sequence storage, work queue.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeLexicographically Smallest String After Applying Operations · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(100 * n^2) = O(n^2)# Space: O(1) class Solution(object):    def findLexSmallestString(self, s, a, b):        """        :type s: str        :type a: int        :type b: int        :rtype: str        """        def less(s, i, j):            for k in xrange(len(s)):                if s[(k+i)%len(s)] != s[(k+j)%len(s)]:                    return s[(k+i)%len(s)] < s[(k+j)%len(s)]            return False         s = list(s)        result = s[:]        even = [False]*10        while not even[int(s[0])]:  # at most O(10) times            even[int(s[0])] = True            odd = [False]*10            while not odd[int(s[1])]:  # at most O(10) times                odd[int(s[1])] = True                best_rotate = 0                lookup = [False]*len(s)                i = b                while not lookup[i]:  # find best rotation, at most O(n) times                    lookup[i] = True                    if less(s, i, best_rotate):  # O(n) time                        best_rotate = i                    i = (i+b)%len(s)                result = min(result, s[best_rotate:] + s[:best_rotate])                for k in xrange(1, len(s), 2):  # flip odd index                    s[k] = str((int(s[k])+a) % 10)            if b%2:  # if rotate length is odd, even index could be also flipped                for k in xrange(0, len(s), 2):  # flip even index                    s[k] = str((int(s[k])+a) % 10)        return "".join(result)  # Time:  O(100 * n^2), at most O(100n) strings and each compare costs O(n)# Space: O(n^2)import collections  class Solution2(object):    def findLexSmallestString(self, s, a, b):        """        :type s: str        :type a: int        :type b: int        :rtype: str        """        q, lookup, result = collections.deque([s]), {s}, s        while q:            curr = q.popleft()            if curr < result:                result = curr            add_a = list(curr)                for i, c in enumerate(add_a):                if i%2:                    add_a[i] = str((int(c)+a) % 10)            add_a = "".join(add_a)                    if add_a not in lookup:                lookup.add(add_a)                q.append(add_a)            rotate_b = curr[b:] + curr[:b]            if rotate_b not in lookup:                lookup.add(rotate_b)                q.append(rotate_b)        return result 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗