|
@@ -1,34 +1,60 @@
|
1
|
1
|
package rocks.zipcode.quiz5.fundamentals;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+import java.util.HashSet;
|
|
6
|
+import java.util.Set;
|
|
7
|
+
|
3
|
8
|
/**
|
4
|
9
|
* @author leon on 21/12/2018.
|
5
|
10
|
*/
|
6
|
11
|
public class StringUtils {
|
7
|
|
- public static Character getMiddleCharacter(String string) {
|
8
|
|
- return null;
|
|
12
|
+ public static Character getMiddleCharacter(String string){
|
|
13
|
+ return string.charAt(string.length()/2);
|
9
|
14
|
}
|
10
|
15
|
|
11
|
16
|
public static String capitalizeMiddleCharacter(String str) {
|
12
|
|
- return null;
|
|
17
|
+ StringBuilder sb = new StringBuilder(str);
|
|
18
|
+ sb.setCharAt(str.length() / 2, Character.toUpperCase(getMiddleCharacter(str)));
|
|
19
|
+ return sb.toString();
|
13
|
20
|
}
|
14
|
21
|
|
15
|
22
|
public static String lowerCaseMiddleCharacter(String str) {
|
16
|
|
- return null;
|
|
23
|
+ StringBuilder sb = new StringBuilder(str);
|
|
24
|
+ sb.setCharAt(str.length() / 2, Character.toLowerCase(getMiddleCharacter(str)));
|
|
25
|
+ return sb.toString();
|
17
|
26
|
}
|
18
|
27
|
|
19
|
28
|
public static Boolean isIsogram(String str) {
|
20
|
|
- return null;
|
|
29
|
+ String[] arr = str.split("");
|
|
30
|
+ Set<String> set = new HashSet<>(Arrays.asList(arr));
|
|
31
|
+ return (str.length() == set.size());
|
21
|
32
|
}
|
22
|
33
|
|
23
|
34
|
public static Boolean hasDuplicateConsecutiveCharacters(String str) {
|
24
|
|
- return null;
|
|
35
|
+ char[] charArr = str.toCharArray();
|
|
36
|
+ for(int i = 0; i < charArr.length - 1; i++)
|
|
37
|
+ if(charArr[i]==charArr[i+1])
|
|
38
|
+ return true;
|
|
39
|
+ return false;
|
25
|
40
|
}
|
26
|
41
|
|
27
|
42
|
public static String removeConsecutiveDuplicateCharacters(String str) {
|
28
|
|
- return null;
|
|
43
|
+ Set<Integer> set = new HashSet<>();
|
|
44
|
+ char[] array = str.toCharArray();
|
|
45
|
+ StringBuilder sb = new StringBuilder(str);
|
|
46
|
+ for(int i = 1; i < array.length; i++)
|
|
47
|
+ if(array[i-1] == array[i]) {
|
|
48
|
+ set.add(i - 1);
|
|
49
|
+ set.add(i);
|
|
50
|
+ }
|
|
51
|
+ int numDeleted = 0;
|
|
52
|
+ for(Integer num : set)
|
|
53
|
+ sb.deleteCharAt(num - numDeleted++);
|
|
54
|
+ return sb.toString();
|
29
|
55
|
}
|
30
|
56
|
|
31
|
57
|
public static String invertCasing(String str) {
|
32
|
|
- return null;
|
|
58
|
+ return org.apache.commons.lang3.StringUtils.swapCase(str);
|
33
|
59
|
}
|
34
|
60
|
}
|