|
@@ -1,5 +1,8 @@
|
1
|
1
|
package com.zipcodewilmington;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+
|
3
|
6
|
/**
|
4
|
7
|
* Created by leon on 1/29/18.
|
5
|
8
|
*/
|
|
@@ -9,7 +12,8 @@ public class StringArrayUtils {
|
9
|
12
|
* @return first element of specified array
|
10
|
13
|
*/ // TODO
|
11
|
14
|
public static String getFirstElement(String[] array) {
|
12
|
|
- return null;
|
|
15
|
+
|
|
16
|
+ return array[0];
|
13
|
17
|
}
|
14
|
18
|
|
15
|
19
|
/**
|
|
@@ -17,7 +21,8 @@ public class StringArrayUtils {
|
17
|
21
|
* @return second element in specified array
|
18
|
22
|
*/
|
19
|
23
|
public static String getSecondElement(String[] array) {
|
20
|
|
- return null;
|
|
24
|
+
|
|
25
|
+ return array[1];
|
21
|
26
|
}
|
22
|
27
|
|
23
|
28
|
/**
|
|
@@ -25,7 +30,8 @@ public class StringArrayUtils {
|
25
|
30
|
* @return last element in specified array
|
26
|
31
|
*/ // TODO
|
27
|
32
|
public static String getLastElement(String[] array) {
|
28
|
|
- return null;
|
|
33
|
+
|
|
34
|
+ return array[(array.length - 1)];
|
29
|
35
|
}
|
30
|
36
|
|
31
|
37
|
/**
|
|
@@ -33,7 +39,7 @@ public class StringArrayUtils {
|
33
|
39
|
* @return second to last element in specified array
|
34
|
40
|
*/ // TODO
|
35
|
41
|
public static String getSecondToLastElement(String[] array) {
|
36
|
|
- return null;
|
|
42
|
+ return array[(array.length - 2)];
|
37
|
43
|
}
|
38
|
44
|
|
39
|
45
|
/**
|
|
@@ -42,6 +48,11 @@ public class StringArrayUtils {
|
42
|
48
|
* @return true if the array contains the specified `value`
|
43
|
49
|
*/ // TODO
|
44
|
50
|
public static boolean contains(String[] array, String value) {
|
|
51
|
+
|
|
52
|
+ for (int i = 0; i < array.length; i++)
|
|
53
|
+ if (value.equals(array[i])) {
|
|
54
|
+ return true;
|
|
55
|
+ }
|
45
|
56
|
return false;
|
46
|
57
|
}
|
47
|
58
|
|
|
@@ -50,7 +61,13 @@ public class StringArrayUtils {
|
50
|
61
|
* @return an array with identical contents in reverse order
|
51
|
62
|
*/ // TODO
|
52
|
63
|
public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
64
|
+ String[] newStr = new String[array.length];
|
|
65
|
+ int n = 0;
|
|
66
|
+ for (int i = array.length - 1; i >= 0; i--) {
|
|
67
|
+ newStr[n] = array[i];
|
|
68
|
+ n++;
|
|
69
|
+ }
|
|
70
|
+ return newStr;
|
54
|
71
|
}
|
55
|
72
|
|
56
|
73
|
/**
|
|
@@ -58,7 +75,16 @@ public class StringArrayUtils {
|
58
|
75
|
* @return true if the order of the array is the same backwards and forwards
|
59
|
76
|
*/ // TODO
|
60
|
77
|
public static boolean isPalindromic(String[] array) {
|
61
|
|
- return false;
|
|
78
|
+ String[] newStr = Arrays.copyOf(array, array.length);
|
|
79
|
+ boolean same = false;
|
|
80
|
+ int n = 0;
|
|
81
|
+ for (int i = array.length - 1; i >= 0; i--) {
|
|
82
|
+ if (array[i] != newStr[n]) {
|
|
83
|
+ return false;
|
|
84
|
+ }
|
|
85
|
+ n++;
|
|
86
|
+ }
|
|
87
|
+ return true;
|
62
|
88
|
}
|
63
|
89
|
|
64
|
90
|
/**
|
|
@@ -66,6 +92,24 @@ public class StringArrayUtils {
|
66
|
92
|
* @return true if each letter in the alphabet has been used in the array
|
67
|
93
|
*/ // TODO
|
68
|
94
|
public static boolean isPangramic(String[] array) {
|
|
95
|
+
|
|
96
|
+ String str = String.join(",", array);
|
|
97
|
+ String alphaStr = "abcdefghijklmnopqrstuvwxyz";
|
|
98
|
+ char[] alphaArray = alphaStr.toCharArray();
|
|
99
|
+ char[] charArray = str.toCharArray();
|
|
100
|
+
|
|
101
|
+ int count = 0;
|
|
102
|
+ for (int i = 0; i < alphaArray.length; i++) {
|
|
103
|
+ for (int j = 0; j < charArray.length; j++) {
|
|
104
|
+ if (alphaArray[i] == charArray[j]) {
|
|
105
|
+ count++;
|
|
106
|
+ //break;
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ if (count >= 26) {
|
|
111
|
+ return true;
|
|
112
|
+ }
|
69
|
113
|
return false;
|
70
|
114
|
}
|
71
|
115
|
|
|
@@ -75,7 +119,14 @@ public class StringArrayUtils {
|
75
|
119
|
* @return number of occurrences the specified `value` has occurred
|
76
|
120
|
*/ // TODO
|
77
|
121
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
122
|
+ int counter = 0;
|
|
123
|
+ for (int i = 0; i < array.length; i++) {
|
|
124
|
+ if (array[i] == value) {
|
|
125
|
+ counter++;
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ return counter;
|
79
|
130
|
}
|
80
|
131
|
|
81
|
132
|
/**
|
|
@@ -84,7 +135,18 @@ public class StringArrayUtils {
|
84
|
135
|
* @return array with identical contents excluding values of `value`
|
85
|
136
|
*/ // TODO
|
86
|
137
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
|
138
|
+ String[] newStr = new String[array.length];
|
|
139
|
+ int n = 0;
|
|
140
|
+ for (int i = 0; i < array.length; i++) {
|
|
141
|
+ if (array[i] != valueToRemove) {
|
|
142
|
+ newStr[n] = array[i];
|
|
143
|
+ n++;
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ }
|
|
147
|
+ return Arrays.copyOf(newStr, n);
|
|
148
|
+
|
|
149
|
+
|
88
|
150
|
}
|
89
|
151
|
|
90
|
152
|
/**
|
|
@@ -92,16 +154,56 @@ public class StringArrayUtils {
|
92
|
154
|
* @return array of Strings with consecutive duplicates removes
|
93
|
155
|
*/ // TODO
|
94
|
156
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
|
157
|
+ int counter = 0;
|
|
158
|
+ ArrayList<String> temp = new ArrayList<>();
|
|
159
|
+ temp.add(array[0]);
|
|
160
|
+
|
|
161
|
+ for (int i=1;i<array.length;i++) {
|
|
162
|
+ if (!array[i].equals(temp.get(counter))) {
|
|
163
|
+ temp.add(array[i]);
|
|
164
|
+ counter++;
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ }
|
|
168
|
+ String[] answer = new String[temp.size()];
|
|
169
|
+ temp.toArray(answer);
|
|
170
|
+
|
|
171
|
+ return answer;
|
96
|
172
|
}
|
97
|
173
|
|
|
174
|
+
|
|
175
|
+
|
98
|
176
|
/**
|
99
|
177
|
* @param array array of chars
|
100
|
178
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
179
|
*/ // TODO
|
102
|
180
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
104
|
|
- }
|
105
|
181
|
|
|
182
|
+ int counter = 0;
|
|
183
|
+ ArrayList<String> temp = new ArrayList<>();
|
|
184
|
+ String str = array[0];
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+ for (int i=1;i<array.length;i++) {
|
|
188
|
+ //if (array[i-1].charAt(0)!=(array[counter].charAt(0))) {
|
106
|
189
|
|
|
190
|
+ if (array[i].equals(array[i-1])) {
|
|
191
|
+ str=str.concat(array[i]);
|
|
192
|
+
|
|
193
|
+ } else {
|
|
194
|
+ temp.add(str);
|
|
195
|
+ str="";
|
|
196
|
+ str=str.concat(array[i]);
|
|
197
|
+ }
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ temp.add(str);
|
|
201
|
+
|
|
202
|
+ array = new String[temp.size()];
|
|
203
|
+ temp.toArray(array);
|
|
204
|
+
|
|
205
|
+ return array;
|
|
206
|
+ }
|
107
|
207
|
}
|
|
208
|
+
|
|
209
|
+
|