-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteToSort.java
More file actions
42 lines (33 loc) · 1.04 KB
/
DeleteToSort.java
File metadata and controls
42 lines (33 loc) · 1.04 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
import java.util.ArrayList;
public class DeleteToSort {
private ArrayList<Integer> indices = new ArrayList<Integer>();
private boolean ordered = true;
public DeleteToSort() {}
ArrayList<Integer> minDeletionSize(String [] A) {
indices.clear(); //clears indices in case of multiple function calls
for (int i = 0; i < A.length-1; i++) {
if (A[i].length() != A[i+1].length()) { //if the strings are different lengths, return -1
indices.add(-1);
return indices;
}
}
for (int l = 0; l < A[0].length(); l++) {
for (int w = 0; w < A.length-1; w++) {
if (A[w].charAt(l) > A[w+1].charAt(l)) {
indices.add(l);
ordered = false;
break; //once a non-ordered pair is found, skip to the next column
}
}
}
if (ordered) { //if there is nothing out of order, return empty array
return new ArrayList<Integer>();
}
return indices;
}
public static void main(String[] args) {
String[] A = {"cba", "daf", "ghik"};
DeleteToSort test = new DeleteToSort();
System.out.println(test.minDeletionSize(A));
}
}