Class Solution

java.lang.Object
g2301_2400.s2381_shifting_letters_ii.Solution

public class Solution extends Object
2381 - Shifting Letters II.<p>Medium</p> <p>You are given a string <code>s</code> of lowercase English letters and a 2D integer array <code>shifts</code> where <code>shifts[i] = [start<sub>i</sub>, end<sub>i</sub>, direction<sub>i</sub>]</code>. For every <code>i</code>, <strong>shift</strong> the characters in <code>s</code> from the index <code>start<sub>i</sub></code> to the index <code>end<sub>i</sub></code> ( <strong>inclusive</strong> ) forward if <code>direction<sub>i</sub> = 1</code>, or shift the characters backward if <code>direction<sub>i</sub> = 0</code>.</p> <p>Shifting a character <strong>forward</strong> means replacing it with the <strong>next</strong> letter in the alphabet (wrapping around so that <code>'z'</code> becomes <code>'a'</code>). Similarly, shifting a character <strong>backward</strong> means replacing it with the <strong>previous</strong> letter in the alphabet (wrapping around so that <code>'a'</code> becomes <code>'z'</code>).</p> <p>Return <em>the final string after all such shifts to</em> <code>s</code> <em>are applied</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;abc&rdquo;, shifts = [[0,1,0],[1,2,1],[0,2,1]]</p> <p><strong>Output:</strong> &ldquo;ace&rdquo;</p> <p><strong>Explanation:</strong> Firstly, shift the characters from index 0 to index 1 backward. Now s = &ldquo;zac&rdquo;.</p> <p>Secondly, shift the characters from index 1 to index 2 forward. Now s = &ldquo;zbd&rdquo;.</p> <p>Finally, shift the characters from index 0 to index 2 forward. Now s = &ldquo;ace&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;dztz&rdquo;, shifts = [[0,0,0],[1,1,1]]</p> <p><strong>Output:</strong> &ldquo;catz&rdquo;</p> <p><strong>Explanation:</strong> Firstly, shift the characters from index 0 to index 0 backward. Now s = &ldquo;cztz&rdquo;.</p> <p>Finally, shift the characters from index 1 to index 1 forward. Now s = &ldquo;catz&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length, shifts.length <= 5 * 10<sup>4</sup></code></li> <li><code>shifts[i].length == 3</code></li> <li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < s.length</code></li> <li><code>0 <= direction<sub>i</sub> <= 1</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • shiftingLetters

      public String shiftingLetters(String s, int[][] shifts)