-
Notifications
You must be signed in to change notification settings - Fork 43
/
ReverseVowelsOfAString.java
116 lines (105 loc) · 3.25 KB
/
ReverseVowelsOfAString.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package LeetCodeJava.TwoPointer;
// https://leetcode.com/problems/reverse-vowels-of-a-string/
import java.util.HashSet;
import java.util.Set;
public class ReverseVowelsOfAString {
// V0
// public String reverseVowels(String s) {
//
// if (s == null || s.length() == 0){
// return s;
// }
//
// char[] _array = s.toCharArray();
//
// String vowels = "aeiou";
//
// int j = s.length()-1;
//
// for (int i = 0; i < _array.length; i++){
// String r = String.valueOf(_array[j]);
// String l = String.valueOf(_array[i]);
//
// if (vowels.contains(l) && vowels.contains(r)){
// char _tmp = _array[i];
// _array[j] = _tmp;
// _array[i] = l;
// }
// }
//
// return _array.toString();
// }
// V1
// IDEA : 2 POINTERS
// https://leetcode.com/problems/reverse-vowels-of-a-string/editorial/
// Return true if the character is a vowel (case-insensitive)
boolean isVowel(char c) {
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';
}
// Function to swap characters at index x and y
// NOTE !!! this function
void swap(char[] chars, int x, int y) {
char temp = chars[x];
chars[x] = chars[y];
chars[y] = temp;
}
public String reverseVowels(String s) {
int start = 0;
int end = s.length() - 1;
// Convert String to char array as String is immutable in Java
char[] sChar = s.toCharArray();
// NOTE !!! below tricks
// While we still have characters to traverse
while (start < end) {
// Find the leftmost vowel
while (start < s.length () && !isVowel(sChar[start])) {
start++;
}
// Find the rightmost vowel
while (end >= 0 && !isVowel(sChar[end])) {
end--;
}
// Swap them if start is left of end
if (start < end) {
swap(sChar, start++, end--);
}
}
// Converting char array back to String
return new String(sChar);
}
// V2
// https://leetcode.com/problems/reverse-vowels-of-a-string/solutions/81221/one-pass-java-solution-13ms/
public class Solution {
public String reverseVowels_2(String s) {
char[] list=s.toCharArray();
Set<Character> set=new HashSet<>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');
set.add('A');
set.add('E');
set.add('I');
set.add('O');
set.add('U');
for (int i=0, j=list.length-1; i<j; ) {
if (!set.contains(list[i])) {
i++;
continue;
}
if (!set.contains(list[j])) {
j--;
continue;
}
char temp=list[i];
list[i]=list[j];
list[j]=temp;
i++;
j--;
}
return String.valueOf(list);
}
}
}