-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseVowelsOfAString.java
64 lines (59 loc) · 1.57 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
public class testing
{
public static void main(String[] args)
{
// 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".
String s = "Ae";
int low = 0;
int high = s.length() - 1;
System.out.println(s);
char[] test = s.toCharArray();
while (low <= high)
{
if(high==low)
break;
while (!isVowel(test[low]))
{
if(low == test.length-1)
break;
else
low++;
}
while (!isVowel(test[high]))
{
if (high > low)
{
high--;
}
else
break;
}
if(high >= low) {
char temp = test[low];
test[low] = test[high];
test[high] = temp;
}
low++;
high--;
}
s = new String(test);
System.out.println(s);
}
static boolean isVowel(char theChar)
{
if(theChar == 'a' || theChar == 'e' || theChar=='i' || theChar=='o' || theChar == 'u'
|| theChar == 'A' || theChar == 'E' || theChar=='I' || theChar=='O' || theChar == 'U')
return true;
return false;
}
}