|
|
@@ -3,6 +3,8 @@ import java.util.Arrays;
|
|
3
|
3
|
import java.util.Collections;
|
|
4
|
4
|
import java.util.List;
|
|
5
|
5
|
import java.util.*;
|
|
|
6
|
+import java.lang.StringBuilder;
|
|
|
7
|
+import java.lang.String;
|
|
6
|
8
|
/**
|
|
7
|
9
|
* Created by leon on 1/29/18.
|
|
8
|
10
|
*/
|
|
|
@@ -94,36 +96,43 @@ public class StringArrayUtils {
|
|
94
|
96
|
* @return true if each letter in the alphabet has been used in the array
|
|
95
|
97
|
*/ // TODO
|
|
96
|
98
|
public static boolean isPangramic(String[] array) {
|
|
|
99
|
+
|
|
|
100
|
+ String alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
|
101
|
+ boolean pan = true;
|
|
97
|
102
|
boolean check = false;
|
|
98
|
|
- //Scanner reader = new Scanner();
|
|
99
|
|
- String string = new String();//reader.nextLine();
|
|
100
|
|
- Set character = new HashSet();
|
|
101
|
|
- Set set = new HashSet();
|
|
102
|
|
- for (char c:string.toUpperCase().toCharArray()){
|
|
103
|
103
|
|
|
104
|
|
- if (character.equals(c)){
|
|
105
|
|
- set.add(c);}
|
|
|
104
|
+ for (int i = 0; i>26; i++){
|
|
|
105
|
+
|
|
|
106
|
+ for(String string:array){
|
|
|
107
|
+
|
|
|
108
|
+ if (string.contains(Character.toString(alphabet.charAt(i)))){
|
|
|
109
|
+ check = true;
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ else{ check = false;
|
|
|
113
|
+ }
|
|
|
114
|
+ }
|
|
|
115
|
+ if (check == false){
|
|
|
116
|
+ pan = false;
|
|
|
117
|
+ break;
|
|
|
118
|
+ }
|
|
106
|
119
|
}
|
|
107
|
|
- if (set.size() == 26){
|
|
108
|
|
- check = true;}
|
|
109
|
|
- else {
|
|
110
|
|
- check = false;}
|
|
111
|
|
- return check;
|
|
112
|
|
-
|
|
|
120
|
+ return pan;
|
|
113
|
121
|
}
|
|
114
|
|
-
|
|
115
|
|
- /**
|
|
116
|
|
- * @param array array of String objects
|
|
117
|
|
- * @param value value to check array for
|
|
118
|
|
- * @return number of occurrences the specified `value` has occurred
|
|
119
|
|
- */ // Done
|
|
120
|
|
- public static int getNumberOfOccurrences(String[] array, String value) {
|
|
|
122
|
+ /**
|
|
|
123
|
+ * @param array array of String objects
|
|
|
124
|
+ * @param value value to check array for
|
|
|
125
|
+ * @return number of occurrences the specified `value` has occurred
|
|
|
126
|
+ */ // Done
|
|
|
127
|
+ public static int getNumberOfOccurrences(String[] array, String value) {
|
|
121
|
128
|
int counter = 0;
|
|
122
|
129
|
for (int i = 0; i<array.length; i++){
|
|
123
|
|
- if (array[i] == array[i+1]){
|
|
124
|
|
- counter=counter+1;}
|
|
125
|
|
-
|
|
|
130
|
+ if (array[i] == value){
|
|
|
131
|
+ counter=counter+1;
|
|
126
|
132
|
}
|
|
|
133
|
+
|
|
|
134
|
+ }
|
|
|
135
|
+
|
|
127
|
136
|
return counter;
|
|
128
|
137
|
}
|
|
129
|
138
|
|
|
|
@@ -133,21 +142,30 @@ public class StringArrayUtils {
|
|
133
|
142
|
* @return array with identical contents excluding values of `value`
|
|
134
|
143
|
*/ // Done
|
|
135
|
144
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
136
|
|
- //sort array, create new array, compare i, add new i to new array.
|
|
137
|
|
- Arrays.sort(array);
|
|
138
|
|
- ArrayList fresh = new ArrayList();
|
|
139
|
|
- for (int i=0;i<array.length;i++){
|
|
140
|
|
- if (array[i] != valueToRemove){
|
|
141
|
|
- fresh.add(array[i]);
|
|
|
145
|
+
|
|
|
146
|
+ String [] temp = new String[array.length-(getNumberOfOccurrences(array,valueToRemove))];
|
|
|
147
|
+
|
|
|
148
|
+ for(int i = 0 ,j = 0; i<array.length; i++){
|
|
|
149
|
+ if(!array[i].equals(valueToRemove)){
|
|
|
150
|
+
|
|
|
151
|
+ temp[j] = array[i];
|
|
|
152
|
+ j++;
|
|
142
|
153
|
}
|
|
143
|
|
-
|
|
144
|
154
|
}
|
|
145
|
|
- String[] list = (String[])(fresh.toArray()); //needed to convert to a string array, not a string
|
|
146
|
|
- return list;
|
|
147
|
|
-
|
|
148
|
|
- //String delete = value.replaceAll (Character.toString(charToRemove), "");
|
|
149
|
|
- //return delete;
|
|
150
|
|
- //return list.toString();
|
|
|
155
|
+
|
|
|
156
|
+ return temp;
|
|
|
157
|
+ }
|
|
|
158
|
+
|
|
|
159
|
+ public static int duplicates(String[] array){
|
|
|
160
|
+ String dupes = new String();
|
|
|
161
|
+ int counter = 0;
|
|
|
162
|
+ for (int i=0; i<array.length-1;i++){
|
|
|
163
|
+ if (array[i] == array[i+1]){
|
|
|
164
|
+ counter++;
|
|
|
165
|
+ //dupes += (array[i]);
|
|
|
166
|
+ }
|
|
|
167
|
+ }
|
|
|
168
|
+ return counter;
|
|
151
|
169
|
}
|
|
152
|
170
|
|
|
153
|
171
|
/**
|
|
|
@@ -155,15 +173,16 @@ public class StringArrayUtils {
|
|
155
|
173
|
* @return array of Strings with consecutive duplicates removes
|
|
156
|
174
|
*/ // TODO
|
|
157
|
175
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
158
|
|
- Arrays.sort(array);
|
|
159
|
|
- ArrayList fresh = new ArrayList();
|
|
160
|
|
- for (int i=0; i<array.length; i++){
|
|
161
|
|
- if (array[i] != array[i-1] && array[i] != array[i+1]) {
|
|
162
|
|
- fresh.add(array[i]);
|
|
|
176
|
+ String [] temp = new String[array.length-duplicates(array)];
|
|
|
177
|
+ int j = 0;
|
|
|
178
|
+ for(int i = 0; i<array.length-1; i++){
|
|
|
179
|
+ if(array[i] != array[i+1]){
|
|
|
180
|
+ temp[j] = array[i];
|
|
|
181
|
+ j++;
|
|
163
|
182
|
}
|
|
164
|
183
|
}
|
|
165
|
|
- String[] list = (String[])(fresh.toArray());
|
|
166
|
|
- return list;
|
|
|
184
|
+ temp[temp.length-1] = array[array.length-1];
|
|
|
185
|
+ return temp;
|
|
167
|
186
|
}
|
|
168
|
187
|
|
|
169
|
188
|
/**
|
|
|
@@ -171,7 +190,21 @@ public class StringArrayUtils {
|
|
171
|
190
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
172
|
191
|
*/ // TODO
|
|
173
|
192
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
174
|
|
- return null;
|
|
|
193
|
+ String[] pack = new String[array.length-duplicates(array)];
|
|
|
194
|
+ int j = 0;
|
|
|
195
|
+ pack[0]=array[0];
|
|
|
196
|
+ for(int i=1; i<array.length; i++){
|
|
|
197
|
+
|
|
|
198
|
+ if (array[i] == array[i-1]){
|
|
|
199
|
+ pack[j] += array[i];
|
|
|
200
|
+ }
|
|
|
201
|
+ else{
|
|
|
202
|
+ j++;
|
|
|
203
|
+ pack[j]=array[i];
|
|
|
204
|
+ }
|
|
|
205
|
+ }
|
|
|
206
|
+
|
|
|
207
|
+ return pack;
|
|
175
|
208
|
}
|
|
176
|
209
|
|
|
177
|
210
|
}
|