-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsolution1.js
More file actions
41 lines (39 loc) · 805 Bytes
/
solution1.js
File metadata and controls
41 lines (39 loc) · 805 Bytes
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
35
36
37
38
39
40
41
/**
* https://leetcode-cn.com/problems/reverse-vowels-of-a-string/
*
* 345. 反转字符串中的元音字母
*
* Easy
*
* 95.35%
* 43.37%
*/
const reverseVowels = s => {
const max = s.length
s = s.split('')
const result = []
const keys = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])
let start = 0
let end = max - 1
while (start < end) {
const isStart = keys.has(s[start])
const isEnd = keys.has(s[end])
if (isStart && isEnd) {
[s[start], s[end]] = [s[end], s[start]]
start++
end--
continue
}
if (isStart) {
end--
continue
}
if (isEnd) {
start++
continue
}
start++
end--
}
return s.join('')
}