-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path345.ReverseVowelsOfAString.cpp
67 lines (63 loc) · 1.35 KB
/
345.ReverseVowelsOfAString.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
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
/*
* @lc app=leetcode id=345 lang=cpp
*
* [345] Reverse Vowels of a String
*
* https://leetcode.com/problems/reverse-vowels-of-a-string/description/
*
* algorithms
* Easy (44.08%)
* Likes: 746
* Dislikes: 1210
* Total Accepted: 233.2K
* Total Submissions: 526K
* 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".
*
*
*
*/
// @lc code=start
#include <string>
#include <ctype.h>
class Solution {
public:
std::string reverseVowels(std::string s) {
int front = 0, back = s.length() - 1;
while (front < back && !isVowel(s[front])) front++;
while (front < back && !isVowel(s[back])) back--;
while (front < back) {
std::swap(s[front], s[back]);
front++; back--;
while (front < back && !isVowel(s[front])) front++;
while (front < back && !isVowel(s[back])) back--;
}
return s;
}
private:
bool isVowel(char letter) {
letter = tolower(letter);
return (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u');
}
};
// @lc code=end