3823. Reverse Letters Then Special Characters in a String
Description
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']:- Reversing them gives
['f', 'a', 'd', 'c', 'b', 'e'] sbecomes")fad#cb@e("
- Reversing them gives
- βββββββThe special characters in the string are
[')', '#', '@', '(']:- Reversing them gives
['(', '@', '#', ')'] sbecomes"(fad@cb#e)"
- Reversing them gives
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 <= 100sconsists only of lowercase English letters and the special characters in"!@#$%^&*()".
Solutions
Solution 1
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |