937. Reorder Data in Log Files
Easy
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.
We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.
Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order.
Return the final order of the logs.
Example 1:
Input: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"] Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
Constraints:
0 <= logs.length <= 100
3 <= logs[i].length <= 100
logs[i]
is guaranteed to have an identifier, and a word after the identifier.
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 | class Solution { class Comparator1 implements Comparator<String> { @Override public int compare(String l1, String l2) { String[] s1 = l1.split(" ",2); String[] s2 = l2.split(" ",2); if(!s1[1].equals(s2[1])){ return s1[1].compareTo(s2[1]); }else{ return s1[0].compareTo(s2[0]); } } } boolean isDLog(String str){ String[] ss = str.split(" "); if(Character.isDigit(ss[1].charAt(0))) return true; else return false; } public String[] reorderLogFiles(String[] logs) { List<String> dLogs = new ArrayList<>(); List<String> lLogs = new ArrayList<>(); for(String l : logs){ if(isDLog(l)) dLogs.add(l); else lLogs.add(l); } lLogs.sort(new Comparator1()); lLogs.addAll(dLogs); return lLogs.toArray(new String[0]); } } |
No comments:
Post a Comment