-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path345.reverse-vowels-of-a-string.js
73 lines (69 loc) · 1.23 KB
/
345.reverse-vowels-of-a-string.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* @lc app=leetcode id=345 lang=javascript
*
* [345] Reverse Vowels of a String
*
* https://leetcode.com/problems/reverse-vowels-of-a-string/description/
*
* algorithms
* Easy (41.17%)
* Total Accepted: 150.4K
* Total Submissions: 364.9K
* Testcase Example: '"hello"'
*
* Write a function that takes a string as input and reverse only the vowels of
* a string.
*
* Example 1:
*
*
* Input: "hello"
* Output: "holle"
*
*
*
* Example 2:
*
*
* Input: "leetcode"
* Output: "leotcede"
*
*
* Note:
* The vowels does not include the letter "y".
*
*
*
*/
/**
* @param {string} s
* @return {string}
*/
const vowelString = "aeiouAEIOU";
function getVowelArr(len, arr) {
var vowelArr = [];
for (var i = 0; i < len; i++) {
if (vowelString.includes(arr[i])) {
vowelArr.push(arr[i]);
}
}
return vowelArr;
}
var reverseVowels = function (s) {
var arr = s.split(""),
len = arr.length;
//first in last out, that why reverse order
/*
time complexity 2n,
*/
let vowelStack = getVowelArr(len, arr);
var ansStr = "";
for (var i = 0; i < len; i++) {
if (vowelString.includes(arr[i])) {
ansStr += vowelStack.pop();
} else {
ansStr += arr[i];
}
}
return ansStr;
};