-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversalAlgorithms.java
More file actions
executable file
·69 lines (62 loc) · 2.15 KB
/
Copy pathReversalAlgorithms.java
File metadata and controls
executable file
·69 lines (62 loc) · 2.15 KB
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
public class ReversalAlgorithms{
public static void reverseSubarray (int start, int end, int[] items){
for (; start < end ; start++, end--){
int temp = items [start];
items[start] = items [end];
items[end] = temp;
}
}
public static void reverseAscendingSubarrays (int[] items){
int start,end = 0;
for (start = 1; start < items.length; start++){
if (items[start] > items[start -1] ){
end++;
}
else{
reverseSubarray(start-end-1, start-1, items);
end = 0;
}
}
if (end > 0){
reverseSubarray(start-end-1, start-1, items);
}
}
public static String pancakeScramble(String text){
String str1 = "";
String str2 = "";
int charPos = 2;
for(int i = 0; i < text.length() - 1; i++){
str1 = new StringBuilder(text.substring(0,charPos)).reverse().toString();
str2 = text.substring(charPos, text.length());
text = str1 + str2;
charPos ++;
}
return text;
}
static boolean isVowel(char c) {
return (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i'
|| c == 'I' || c == 'o' || c == 'O' || c == 'u'|| c == 'U');
}
public static String reverseVowels(String text) {
int indexVowels = 0;
char[] textArr = text.toCharArray();
String vowels = "";
for (int i = 0; i < textArr.length; i++) {
if (isVowel(textArr[i])) {
indexVowels ++;
vowels += textArr[i];
}
}
for (int i = 0; i < textArr.length; i++) {
if (isVowel(textArr[i])) {
if(Character.isUpperCase(textArr[i])){
textArr[i] = Character.toUpperCase(vowels.charAt(--indexVowels));
}
if(Character.isLowerCase(textArr[i])){
textArr[i] = Character.toLowerCase(vowels.charAt(--indexVowels));
}
}
}
return new String(textArr);
}
}