-
Notifications
You must be signed in to change notification settings - Fork 0
/
917-reverse-only-letters.cpp
34 lines (29 loc) · 1.09 KB
/
917-reverse-only-letters.cpp
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
27
28
29
30
31
32
33
34
// Title: Reverse Only Letters
// Description:
// Given a string s, reverse the string according to the following rules:
// All the characters that are not English letters remain in the same position.
// All the English letters (lowercase or uppercase) should be reversed.
// Return s after reversing it.
// Link: https://leetcode.com/problems/reverse-only-letters/
// Time complexity: O(n)
// Space complexity: O(n)
class Solution {
public:
string reverseOnlyLetters(string s) {
// copy the original string to modify on
std::string result = s;
// read the original string backward
std::reverse_iterator it = s.rbegin();
for (char &c: result) {
// leave the non-letter alone
if (!std::isalpha(c)) continue;
// skip any non-letters when reading backward
while (!std::isalpha(*it)) ++it;
// replace the letter with the reversed one
c = *it;
// advance the reversed iterator
++it;
}
return result;
}
};