Skip to content

Latest commit

 

History

History

3823-reverse-letters-then-special-characters-in-a-string

README.md

Easy


You are given a string s consisting of lowercase English letters and special characters.

Your task is to perform these in order:

  • Reverse the lowercase letters and place them back into the positions originally occupied by letters.
  • Reverse the special characters and place them back into the positions originally occupied by special characters.

Return the resulting string after performing the reversals.

 

Example 1:

Input: s = ")ebc#da@f("

Output: "(fad@cb#e)"

Explanation:

  • The letters in the string are ['e', 'b', 'c', 'd', 'a', 'f']:
    <ul>
    	<li>Reversing them gives <code>[&#39;f&#39;, &#39;a&#39;, &#39;d&#39;, &#39;c&#39;, &#39;b&#39;, &#39;e&#39;]</code></li>
    	<li><code>s</code> becomes <code>&quot;)fad#cb@e(&quot;</code></li>
    </ul>
    </li>
    <li>​​​​​​​The special characters in the string are <code>[&#39;)&#39;, &#39;#&#39;, &#39;@&#39;, &#39;(&#39;]</code>:
    <ul>
    	<li>Reversing them gives <code>[&#39;(&#39;, &#39;@&#39;, &#39;#&#39;, &#39;)&#39;]</code></li>
    	<li><code>s</code> becomes <code><span class="example-io">&quot;</span>(fad@cb#e)<span class="example-io">&quot;</span></code></li>
    </ul>
    </li>
    

Example 2:

Input: s = "z"

Output: "z"

Explanation:

The string contains only one letter, and reversing it does not change the string. There are no special characters.

Example 3:

Input: s = "!@#$%^&*()"

Output: ")(*&^%$#@!"

Explanation:

The string contains no letters. The string contains all special characters, so reversing the special characters reverses the whole string.

 

Constraints:

  • 1 <= s.length <= 100
  • s consists only of lowercase English letters and the special characters in "!@#$%^&*()".