|
| 1 | +<h2><a href="https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations">1747. Lexicographically Smallest String After Applying Operations</a></h2><h3>Medium</h3><hr><p>You are given a string <code>s</code> of <strong>even length</strong> consisting of digits from <code>0</code> to <code>9</code>, and two integers <code>a</code> and <code>b</code>.</p> |
| 2 | + |
| 3 | +<p>You can apply either of the following two operations any number of times and in any order on <code>s</code>:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Add <code>a</code> to all odd indices of <code>s</code> <strong>(0-indexed)</strong>. Digits post <code>9</code> are cycled back to <code>0</code>. For example, if <code>s = "3456"</code> and <code>a = 5</code>, <code>s</code> becomes <code>"3951"</code>.</li> |
| 7 | + <li>Rotate <code>s</code> to the right by <code>b</code> positions. For example, if <code>s = "3456"</code> and <code>b = 1</code>, <code>s</code> becomes <code>"6345"</code>.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>Return <em>the <strong>lexicographically smallest</strong> string you can obtain by applying the above operations any number of times on</em> <code>s</code>.</p> |
| 11 | + |
| 12 | +<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears earlier in the alphabet than the corresponding letter in <code>b</code>. For example, <code>"0158"</code> is lexicographically smaller than <code>"0190"</code> because the first position they differ is at the third letter, and <code>'5'</code> comes before <code>'9'</code>.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<pre> |
| 18 | +<strong>Input:</strong> s = "5525", a = 9, b = 2 |
| 19 | +<strong>Output:</strong> "2050" |
| 20 | +<strong>Explanation:</strong> We can apply the following operations: |
| 21 | +Start: "5525" |
| 22 | +Rotate: "2555" |
| 23 | +Add: "2454" |
| 24 | +Add: "2353" |
| 25 | +Rotate: "5323" |
| 26 | +Add: "5222" |
| 27 | +Add: "5121" |
| 28 | +Rotate: "2151" |
| 29 | +Add: "2050" |
| 30 | +There is no way to obtain a string that is lexicographically smaller than "2050". |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> s = "74", a = 5, b = 1 |
| 37 | +<strong>Output:</strong> "24" |
| 38 | +<strong>Explanation:</strong> We can apply the following operations: |
| 39 | +Start: "74" |
| 40 | +Rotate: "47" |
| 41 | +Add: "42" |
| 42 | +Rotate: "24" |
| 43 | +There is no way to obtain a string that is lexicographically smaller than "24". |
| 44 | +</pre> |
| 45 | + |
| 46 | +<p><strong class="example">Example 3:</strong></p> |
| 47 | + |
| 48 | +<pre> |
| 49 | +<strong>Input:</strong> s = "0011", a = 4, b = 2 |
| 50 | +<strong>Output:</strong> "0011" |
| 51 | +<strong>Explanation:</strong> There are no sequence of operations that will give us a lexicographically smaller string than "0011". |
| 52 | +</pre> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | +<p><strong>Constraints:</strong></p> |
| 56 | + |
| 57 | +<ul> |
| 58 | + <li><code>2 <= s.length <= 100</code></li> |
| 59 | + <li><code>s.length</code> is even.</li> |
| 60 | + <li><code>s</code> consists of digits from <code>0</code> to <code>9</code> only.</li> |
| 61 | + <li><code>1 <= a <= 9</code></li> |
| 62 | + <li><code>1 <= b <= s.length - 1</code></li> |
| 63 | +</ul> |
0 commit comments