You have an array of logs. Each log is a space delimited string of words.
For each log, the first word in each log is an alphanumeric identifier. Then, either:
Each word after the identifier will consist only of lowercase letters, or;
Each word after the identifier will consist only of digits.
so this problem is a lexicographically order problem.
given an array of strings. return a new array in the right order.
so this problem mainly wants us to wrote a comparator.
class Solution {
public String[] reorderLogFiles(String[] logs) {
Arrays.sort(logs, (s1, s2) -> {
String[] split1 = s1.split(" ", 2);
String[] split2 = s2.split(" ", 2);
boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
if(!isDigit1 && !isDigit2) {
// both letter-logs.
int comp = split1[1].compareTo(split2[1]);
if (comp == 0) return split1[0].compareTo(split2[0]);
else return comp;
} else if (isDigit1 && isDigit2) {
// both digit-logs. So keep them in original order
return 0;
} else if (isDigit1 && !isDigit2) {
// first is digit, second is letter. bring letter to forward.
return 1;
} else {
//first is letter, second is digit. keep them in this order.
return -1;
}
});
return logs;
}
}