How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: Medium
Topics: String Manipulation, BFS, Math, Simulation
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:
a to all odd indices of s (0-indexed).Example:
Ifs = "3456"anda = 5,
thensbecomes "3951".
s to the right by b positions.
Example:
Ifs = "3456"andb = 1,
thensbecomes "6345".
Your task is to 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 they differ,
the character in a is smaller than the character in b.
Example:
"0158" is lexicographically smaller than "0190"
because they differ at the third character ('5' < '9').
Input: s = "5525", a = 9, b = 2
Output: "2050"
Explanation: We can apply the following operations:
| Step | Operation | Result |
|---|---|---|
| 1 | Start | 5525 |
| 2 | Rotate | 2555 |
| 3 | Add | 2454 |
| 4 | Add | 2353 |
| 5 | Rotate | 5323 |
| 6 | Add | 5222 |
| 7 | Add | 5121 |
| 8 | Rotate | 2151 |
| 9 | Add | 2050 โ |
There is no way to obtain a string smaller than "2050".
Input: s = "74", a = 5, b = 1
Output: "24"
Explanation:
Start โ "74"
Rotate โ "47"
Add โ "42"
Rotate โ "24"
There is no smaller string than "24".
Input: s = "0011", a = 4, b = 2
Output: "0011"
Explanation:
No sequence of operations can give a smaller string than "0011".
๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.