-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path345. Reverse Vowels of a String.c
67 lines (48 loc) · 1.24 KB
/
345. Reverse Vowels of a String.c
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
/*
345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
The vowels does not include the letter "y".
*/
#define IS_VOWEL(C) ((C) == 'a' || (C) == 'A' || \
(C) == 'e' || (C) == 'E' || \
(C) == 'i' || (C) == 'I' || \
(C) == 'o' || (C) == 'O' || \
(C) == 'u' || (C) == 'U')
char* reverseVowels(char* s) {
char *a, *b;
if (!s || !*s) return s;
a = s;
b = &s[strlen(s) - 1];
while (a < b) {
if (IS_VOWEL(*a) && IS_VOWEL(*b)) {
// swap
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
a ++;
b --;
} else if (IS_VOWEL(*a)) {
b --;
} else if (IS_VOWEL(*b)) {
a ++;
} else {
a ++;
b --;
}
}
return s;
}
/*
Difficulty:Easy
Total Accepted:82.8K
Total Submissions:215.1K
Companies Google
Related Topics Two Pointers String
Similar Questions
Reverse String
*/