Class Solution

java.lang.Object
g1901_2000.s2000_reverse_prefix_of_word.Solution

public class Solution extends Object
2000 - Reverse Prefix of Word.<p>Easy</p> <p>Given a <strong>0-indexed</strong> string <code>word</code> and a character <code>ch</code>, <strong>reverse</strong> the segment of <code>word</code> that starts at index <code>0</code> and ends at the index of the <strong>first occurrence</strong> of <code>ch</code> ( <strong>inclusive</strong> ). If the character <code>ch</code> does not exist in <code>word</code>, do nothing.</p> <ul> <li>For example, if <code>word = &quot;abcdefd&quot;</code> and <code>ch = &quot;d&quot;</code>, then you should <strong>reverse</strong> the segment that starts at <code>0</code> and ends at <code>3</code> ( <strong>inclusive</strong> ). The resulting string will be <code>&quot;dcbaefd&quot;</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> word = &ldquo;abcdefd&rdquo;, ch = &ldquo;d&rdquo;</p> <p><strong>Output:</strong> &ldquo;dcbaefd&rdquo;</p> <p><strong>Explanation:</strong> The first occurrence of &ldquo;d&rdquo; is at index 3.</p> <p>Reverse the part of word from 0 to 3 (inclusive), the resulting string is &ldquo;dcbaefd&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word = &ldquo;xyxzxe&rdquo;, ch = &ldquo;z&rdquo;</p> <p><strong>Output:</strong> &ldquo;zxyxxe&rdquo;</p> <p><strong>Explanation:</strong> The first and only occurrence of &ldquo;z&rdquo; is at index 3.</p> <p>Reverse the part of word from 0 to 3 (inclusive), the resulting string is &ldquo;zxyxxe&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> word = &ldquo;abcd&rdquo;, ch = &ldquo;z&rdquo;</p> <p><strong>Output:</strong> &ldquo;abcd&rdquo;</p> <p><strong>Explanation:</strong> &ldquo;z&rdquo; does not exist in word.</p> <p>You should not do any reverse operation, the resulting string is &ldquo;abcd&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= word.length <= 250</code></li> <li><code>word</code> consists of lowercase English letters.</li> <li><code>ch</code> is a lowercase English letter.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • reversePrefix

      public String reversePrefix(String word, char ch)