Lexicographically Smallest String After Applying Operations - Problem
String Transformation Challenge: You're given a string s of even length consisting only of digits (0-9), along with two integers a and b. Your goal is to find the lexicographically smallest string possible by applying these two operations any number of times:
  1. Add Operation: Add a to all digits at odd indices (1, 3, 5, ...). Digits wrap around after 9 (e.g., 8+3=1, 9+2=1)
  2. Rotate Operation: Rotate the string b positions to the right
Example: If s = "5525", a = 9, b = 2:
  • Add 9 to odd indices: "5525" โ†’ "5424" (5+9=4, 2+9=1)
  • Rotate right by 2: "5424" โ†’ "2454"
Return the lexicographically smallest string achievable through any sequence of these operations.

Input & Output

example_1.py โ€” Basic transformation
$ Input: s = "5525", a = 9, b = 2
โ€บ Output: "2050"
๐Ÿ’ก Note: Starting with "5525", we can apply operations: add 9 to odd indices โ†’ "5424", rotate by 2 โ†’ "2454", continue exploring until we find "2050" as the lexicographically smallest.
example_2.py โ€” Multiple rotations
$ Input: s = "74", a = 5, b = 1
โ€บ Output: "24"
๐Ÿ’ก Note: From "74": add 5 to odd indices โ†’ "79", rotate by 1 โ†’ "97", add 5 โ†’ "92", rotate โ†’ "29", add 5 โ†’ "24". The smallest achievable string is "24".
example_3.py โ€” No improvement possible
$ Input: s = "0011", a = 4, b = 2
โ€บ Output: "0011"
๐Ÿ’ก Note: Starting with "0011", after exploring all possible operations, no string lexicographically smaller than "0011" can be achieved.

Visualization

Tap to expand
String State Exploration5525Initial State5424Add Operation2555Rotate Operation2424425455252050Optimal Result
Understanding the Visualization
1
Start Position
Begin with the initial string configuration
2
Generate States
Apply both operations to create new possible states
3
Track Visited
Mark states as visited to avoid infinite cycles
4
Find Minimum
Keep track of the lexicographically smallest string encountered
Key Takeaway
๐ŸŽฏ Key Insight: Since the string length is finite and digits are bounded (0-9), there are only finitely many reachable states. BFS guarantees we find the lexicographically smallest string by exploring all possibilities systematically.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * 10^n)

In worst case, we might visit all possible digit combinations (10^n) and for each state we do O(n) work

n
2n
โœ“ Linear Growth
Space Complexity
O(10^n)

We store all visited states in a set, which could be up to 10^n different strings

n
2n
โšก Linearithmic Space

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 in
Google 15 Amazon 12 Facebook 8 Microsoft 5
28.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen