Lexicographically Smallest String After Applying Operations - Problem

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:

  • Operation 1: Add a to all odd indices of s (0-indexed). Digits post 9 are cycled back to 0. For example, if s = "3456" and a = 5, s becomes "3951".
  • Operation 2: Rotate s to the right by b positions. For example, if s = "3456" and b = 1, s becomes "6345".

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 letter that appears earlier in the alphabet than the corresponding letter in b.

Input & Output

Example 1 — Basic Case
$ Input: s = "5014", a = 9, b = 2
Output: "0145"
💡 Note: Apply operation 2 to rotate right by 2: "5014" → "1450". Then apply operation 2 again: "1450" → "0145". This gives the lexicographically smallest result.
Example 2 — Only Operation 1 Needed
$ Input: s = "74", a = 5, b = 1
Output: "24"
💡 Note: Apply operation 1 to add 5 to odd indices: "74" → "72" (7+5=12→2). Continue: "72" → "70" → "78" → "76" → "74" → "72"... Then try rotations to get "24".
Example 3 — No Operations Needed
$ Input: s = "0000", a = 1, b = 1
Output: "0000"
💡 Note: The string is already lexicographically smallest possible (all zeros), so no operations improve it.

Constraints

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

Visualization

Tap to expand
Lexicographically Smallest String - BFS INPUT String s = "5014" 5 idx 0 0 idx 1 1 idx 2 4 idx 3 Red = odd indices (affected by add) a = 9 b = 2 Operations: Op1: Add 'a' to odd indices Op2: Rotate right by 'b' Example: "5014" + add --> "5914" "5014" + rotate --> "1450" ALGORITHM STEPS 1 Initialize BFS Queue with s, visited set 2 Explore States Apply both ops to each state 3 Track Minimum Compare strings lexically 4 Return Smallest After all states explored BFS State Tree: "5014" "5914" +add "1450" +rotate ... continues exploring ... FINAL RESULT Smallest String Found: 0 1 4 5 Output: "0145" Verification: One possible path: "5014" --rotate--> "1450" "1450" --rotate--> "5014" "5014" --add--> "5913" ... eventually reaches ... "0145" [OK] Key Insight: BFS explores ALL possible states by applying add and rotate operations repeatedly. Since digits cycle (0-9) and rotations are cyclic, the state space is FINITE. We track visited states to avoid cycles and keep the lexicographically smallest string seen. Time: O(n * 10^n) worst case, Space: O(unique states). TutorialsPoint - Lexicographically Smallest String After Applying Operations | BFS Approach
Asked in
Google 15 Facebook 12 Amazon 8
24.0K Views
Medium Frequency
~25 min Avg. Time
890 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