Welcome to Subscribe On Youtube
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: Simulation
We first store the letters and special characters from string $s$ into two separate lists $a$ and $b$ respectively. Then we traverse the string $s$. If the current position is a letter, we pop the last letter from list $a$ and place it back at that position; otherwise, we pop the last special character from list $b$ and place it back at that position.
After the traversal is complete, we obtain the result string.
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of string $s$.
-
class Solution { public String reverseByType(String s) { StringBuilder a = new StringBuilder(); StringBuilder b = new StringBuilder(); char[] t = s.toCharArray(); for (char c : t) { if (Character.isLetter(c)) { a.append(c); } else { b.append(c); } } int j = a.length(), k = b.length(); for (int i = 0; i < t.length; ++i) { if (Character.isLetter(t[i])) { t[i] = a.charAt(--j); } else { t[i] = b.charAt(--k); } } return new String(t); } } -
class Solution { public: string reverseByType(string s) { string a, b; for (char c : s) { if (isalpha(c)) { a.push_back(c); } else { b.push_back(c); } } int j = a.size(), k = b.size(); for (int i = 0; i < s.size(); ++i) { if (isalpha(s[i])) { s[i] = a[--j]; } else { s[i] = b[--k]; } } return s; } }; -
class Solution: def reverseByType(self, s: str) -> str: a = [] b = [] for c in s: if c.isalpha(): a.append(c) else: b.append(c) return ''.join(a.pop() if c.isalpha() else b.pop() for c in s) -
func reverseByType(s string) string { a := make([]byte, 0) b := make([]byte, 0) t := []byte(s) for _, c := range t { if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') { a = append(a, c) } else { b = append(b, c) } } j, k := len(a), len(b) for i := 0; i < len(t); i++ { if (t[i] >= 'A' && t[i] <= 'Z') || (t[i] >= 'a' && t[i] <= 'z') { j-- t[i] = a[j] } else { k-- t[i] = b[k] } } return string(t) } -
function reverseByType(s: string): string { const a: string[] = []; const b: string[] = []; const t = s.split(''); for (const c of t) { if (/[a-zA-Z]/.test(c)) { a.push(c); } else { b.push(c); } } let j = a.length, k = b.length; for (let i = 0; i < t.length; i++) { if (/[a-zA-Z]/.test(t[i])) { t[i] = a[--j]; } else { t[i] = b[--k]; } } return t.join(''); }