Lexicographically Smallest String After Applying Operations

You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b.

You can apply either of the following two operations any number of times and in any order on s:

  • Add a to all odd indices of s (0-indexed). Digits past 9 are cycled back to 0.
  • Rotate s to the right by b positions.

Return the lexicographically smallest string you can obtain by applying the above operations any number of times on s.

A string a is lexicographically smaller than a string b of the same length if, in the first position where a and b differ, string a has a character that appears earlier in the alphabet than the corresponding character in b.

Example 1
Inputs = "5525", a = 9, b = 2
Output"2050"
After applying a sequence of rotations and additions, the smallest obtainable string is "2050".
Example 2
Inputs = "74", a = 5, b = 1
Output"24"
After rotating and adding as shown, the smallest obtainable string is "24".

Constraints

  • 2 <= s.length <= 100
  • s.length is even.
  • s consists of digits from 0 to 9 only.
  • 1 <= a <= 9
  • 1 <= b <= s.length - 1

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate