-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path345.reverse-vowels-of-a-string.cpp
64 lines (64 loc) · 1.2 KB
/
345.reverse-vowels-of-a-string.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
/*
* @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 (40.28%)
* Total Accepted: 133.6K
* Total Submissions: 331.3K
* 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".
*
*
*
*/
class Solution {
public:
bool isvowel(char c){
if(c <= 'Z' && c>= 'A')
c = 'a' + c-'A';
if(c == 'a' ||c == 'e' ||c == 'i' ||c == 'o' ||c == 'u')
return true;
return false;
}
string reverseVowels(string S) {
string s = S;
int i = 0,j = s.size()-1;
while(i<j){
if(!isvowel(s[i])){
i++; continue;
}
if(!isvowel(s[j])){
j--; continue;
}
// ith and jth is vowel.
swap(s[i], s[j]);
i++;
j--;
}
return s;
}
};