java.lang.Object
g1601_1700.s1625_lexicographically_smallest_string_after_applying_operations.Solution

public class Solution extends Object
1625 - Lexicographically Smallest String After Applying Operations.<p>Medium</p> <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> <p>You can apply either of the following two operations any number of times and in any order on <code>s</code>:</p> <ul> <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 = &quot;3456&quot;</code> and <code>a = 5</code>, <code>s</code> becomes <code>&quot;3951&quot;</code>.</li> <li>Rotate <code>s</code> to the right by <code>b</code> positions. For example, if <code>s = &quot;3456&quot;</code> and <code>b = 1</code>, <code>s</code> becomes <code>&quot;6345&quot;</code>.</li> </ul> <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> <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>&quot;0158&quot;</code> is lexicographically smaller than <code>&quot;0190&quot;</code> because the first position they differ is at the third letter, and <code>'5'</code> comes before <code>'9'</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;5525&rdquo;, a = 9, b = 2</p> <p><strong>Output:</strong> &ldquo;2050&rdquo;</p> <p><strong>Explanation:</strong> We can apply the following operations:</p> <p>Start: &ldquo;5525&rdquo;</p> <p>Rotate: &ldquo;2555&rdquo;</p> <p>Add: &ldquo;2454&rdquo;</p> <p>Add: &ldquo;2353&rdquo;</p> <p>Rotate: &ldquo;5323&rdquo;</p> <p>Add: &ldquo;5222&rdquo;</p> <p>Add: &ldquo;5121&rdquo;</p> <p>Rotate: &ldquo;2151&rdquo;</p> <p>Add: &ldquo;2050&rdquo;</p> <p>There is no way to obtain a string that is lexicographically smaller then &ldquo;2050&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;74&rdquo;, a = 5, b = 1</p> <p><strong>Output:</strong> &ldquo;24&rdquo;</p> <p><strong>Explanation:</strong> We can apply the following operations:</p> <p>Start: &ldquo;74&rdquo;</p> <p>Rotate: &ldquo;47&rdquo;</p> <p>Add: &ldquo;42&rdquo;</p> <p>Rotate: &ldquo;24&rdquo;</p> <p>There is no way to obtain a string that is lexicographically smaller then &ldquo;24&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;0011&rdquo;, a = 4, b = 2</p> <p><strong>Output:</strong> &ldquo;0011&rdquo;</p> <p><strong>Explanation:</strong> There are no sequence of operations that will give us a lexicographically smaller string than &ldquo;0011&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= s.length <= 100</code></li> <li><code>s.length</code> is even.</li> <li><code>s</code> consists of digits from <code>0</code> to <code>9</code> only.</li> <li><code>1 <= a <= 9</code></li> <li><code>1 <= b <= s.length - 1</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findLexSmallestString

      public String findLexSmallestString(String s, int a, int b)