You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0900-0999/0917.Reverse Only Letters/README_EN.md
+40-32Lines changed: 40 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,47 +37,51 @@
37
37
38
38
## Solutions
39
39
40
-
### Solution 1
40
+
### Solution 1: Two Pointers
41
+
42
+
We use two pointers $i$ and $j$ to point to the head and tail of the string respectively. When $i < j$, we continuously move $i$ and $j$ until $i$ points to an English letter and $j$ points to an English letter, then we swap $s[i]$ and $s[j]$. Finally, we return the string.
43
+
44
+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string.
41
45
42
46
<!-- tabs:start -->
43
47
44
48
```python
45
49
classSolution:
46
50
defreverseOnlyLetters(self, s: str) -> str:
47
-
s=list(s)
48
-
i, j =0, len(s) -1
51
+
cs=list(s)
52
+
i, j =0, len(cs) -1
49
53
while i < j:
50
-
while i < j andnots[i].isalpha():
54
+
while i < j andnotcs[i].isalpha():
51
55
i +=1
52
-
while i < j andnots[j].isalpha():
56
+
while i < j andnotcs[j].isalpha():
53
57
j -=1
54
58
if i < j:
55
-
s[i], s[j] =s[j], s[i]
59
+
cs[i], cs[j] =cs[j], cs[i]
56
60
i, j = i +1, j -1
57
-
return''.join(s)
61
+
return"".join(cs)
58
62
```
59
63
60
64
```java
61
65
classSolution {
62
66
publicStringreverseOnlyLetters(Strings) {
63
-
char[] chars= s.toCharArray();
64
-
int i =0, j =s.length()-1;
67
+
char[] cs= s.toCharArray();
68
+
int i =0, j =cs.length -1;
65
69
while (i < j) {
66
-
while (i < j &&!Character.isLetter(chars[i])) {
70
+
while (i < j &&!Character.isLetter(cs[i])) {
67
71
++i;
68
72
}
69
-
while (i < j &&!Character.isLetter(chars[j])) {
73
+
while (i < j &&!Character.isLetter(cs[j])) {
70
74
--j;
71
75
}
72
76
if (i < j) {
73
-
char t =chars[i];
74
-
chars[i] =chars[j];
75
-
chars[j] = t;
77
+
char t =cs[i];
78
+
cs[i] =cs[j];
79
+
cs[j] = t;
76
80
++i;
77
81
--j;
78
82
}
79
83
}
80
-
returnnewString(chars);
84
+
returnnewString(cs);
81
85
}
82
86
}
83
87
```
@@ -88,13 +92,15 @@ public:
88
92
string reverseOnlyLetters(string s) {
89
93
int i = 0, j = s.size() - 1;
90
94
while (i < j) {
91
-
while (i < j && !isalpha(s[i])) ++i;
92
-
while (i < j && !isalpha(s[j])) --j;
93
-
if (i < j) {
94
-
swap(s[i], s[j]);
95
+
while (i < j && !isalpha(s[i])) {
95
96
++i;
97
+
}
98
+
while (i < j && !isalpha(s[j])) {
96
99
--j;
97
100
}
101
+
if (i < j) {
102
+
swap(s[i++], s[j--]);
103
+
}
98
104
}
99
105
return s;
100
106
}
@@ -103,39 +109,41 @@ public:
103
109
104
110
```go
105
111
func reverseOnlyLetters(s string) string {
106
-
ans := []rune(s)
112
+
cs := []rune(s)
107
113
i, j := 0, len(s)-1
108
114
for i < j {
109
-
for i < j && !unicode.IsLetter(ans[i]) {
115
+
for i < j && !unicode.IsLetter(cs[i]) {
110
116
i++
111
117
}
112
-
for i < j && !unicode.IsLetter(ans[j]) {
118
+
for i < j && !unicode.IsLetter(cs[j]) {
113
119
j--
114
120
}
115
121
if i < j {
116
-
ans[i], ans[j] = ans[j], ans[i]
122
+
cs[i], cs[j] = cs[j], cs[i]
117
123
i++
118
124
j--
119
125
}
120
126
}
121
-
return string(ans)
127
+
return string(cs)
122
128
}
123
129
```
124
130
125
131
```ts
126
132
function reverseOnlyLetters(s:string):string {
127
-
const n =s.length;
128
-
let i =0,
129
-
j =n-1;
130
-
let ans = [...s];
133
+
const cs = [...s];
134
+
let [i, j] = [0, cs.length-1];
131
135
while (i<j) {
132
-
while (!/[a-zA-Z]/.test(ans[i]) &&i<j) i++;
133
-
while (!/[a-zA-Z]/.test(ans[j]) &&i<j) j--;
134
-
[ans[i], ans[j]] = [ans[j], ans[i]];
136
+
while (!/[a-zA-Z]/.test(cs[i]) &&i<j) {
137
+
i++;
138
+
}
139
+
while (!/[a-zA-Z]/.test(cs[j]) &&i<j) {
140
+
j--;
141
+
}
142
+
[cs[i], cs[j]] = [cs[j], cs[i]];
135
143
i++;
136
144
j--;
137
145
}
138
-
returnans.join('');
146
+
returncs.join('');
139
147
}
140
148
```
141
149
Collapse file: solution/0900-0999/0917.Reverse Only Letters/Solution.cpp
0 commit comments