java.lang.Object
g1501_1600.s1540_can_convert_string_in_k_moves.Solution

public class Solution extends Object
1540 - Can Convert String in K Moves.<p>Medium</p> <p>Given two strings <code>s</code> and <code>t</code>, your goal is to convert <code>s</code> into <code>t</code> in <code>k</code>moves or less.</p> <p>During the <code>i<sup>th</sup></code> (<code>1 <= i <= k</code>) move you can:</p> <ul> <li>Choose any index <code>j</code> (1-indexed) from <code>s</code>, such that <code>1 <= j <= s.length</code> and <code>j</code> has not been chosen in any previous move, and shift the character at that index <code>i</code> times.</li> <li>Do nothing.</li> </ul> <p>Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that <code>'z'</code> becomes <code>'a'</code>). Shifting a character by <code>i</code> means applying the shift operations <code>i</code> times.</p> <p>Remember that any index <code>j</code> can be picked at most once.</p> <p>Return <code>true</code> if it&rsquo;s possible to convert <code>s</code> into <code>t</code> in no more than <code>k</code> moves, otherwise return <code>false</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;input&rdquo;, t = &ldquo;ouput&rdquo;, k = 9</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> In the 6th move, we shift &lsquo;i&rsquo; 6 times to get &lsquo;o&rsquo;. And in the 7th move we shift &lsquo;n&rsquo; to get &lsquo;u&rsquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;abc&rdquo;, t = &ldquo;bcd&rdquo;, k = 10</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> We need to shift each character in s one time to convert it into t. We can shift &lsquo;a&rsquo; to &lsquo;b&rsquo; during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;aab&rdquo;, t = &ldquo;bbb&rdquo;, k = 27</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> In the 1st move, we shift the first &lsquo;a&rsquo; 1 time to get &lsquo;b&rsquo;. In the 27th move, we shift the second &lsquo;a&rsquo; 27 times to get &lsquo;b&rsquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length, t.length <= 10^5</code></li> <li><code>0 <= k <= 10^9</code></li> <li><code>s</code>, <code>t</code> contain only lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • canConvertString

      public boolean canConvertString(String s, String t, int k)