937. Reorder Data in Log Files LeetCode Solution
In this guide, you will get 937. Reorder Data in Log Files LeetCode Solution with the best time and space complexity. The solution to Reorder Data in Log Files problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Reorder Data in Log Files solution in C++
- Reorder Data in Log Files solution in Java
- Reorder Data in Log Files solution in Python
- Additional Resources

Problem Statement of Reorder Data in Log Files
You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.
There are two types of logs:
Letter-logs: All words (except the identifier) consist of lowercase English letters.
Digit-logs: All words (except the identifier) consist of digits.
Reorder these logs so that:
The letter-logs come before all digit-logs.
The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
The digit-logs maintain their relative ordering.
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″]
Explanation:
The letter-log contents are all different, so their ordering is “art can”, “art zero”, “own kit dig”.
The digit-logs have a relative order of “dig1 8 1 5 1”, “dig2 3 6”.
Example 2:
Input: logs = [“a1 9 2 3 1″,”g1 act car”,”zo4 4 7″,”ab1 off key dog”,”a8 act zoo”]
Output: [“g1 act car”,”a8 act zoo”,”ab1 off key dog”,”a1 9 2 3 1″,”zo4 4 7″]
Constraints:
1 <= logs.length <= 100
3 <= logs[i].length <= 100
All the tokens of logs[i] are separated by a single space.
logs[i] is guaranteed to have an identifier and at least one word after the identifier.
Complexity Analysis
- Time Complexity: O(\Sigma |\texttt{digitLogs[i]}| + \Sigma |\texttt{letterLogs[i]} |\log \Sigma |\texttt{letterLogs[i]}|)
- Space Complexity: O(\Sigma |\texttt{logs[i]}|)
937. Reorder Data in Log Files LeetCode Solution in C++
class Solution {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
vector<string> ans;
vector<string> digitLogs;
vector<pair<string, string>> letterLogs;
for (const string& log : logs) {
const int i = log.find_first_of(' ');
if (isdigit(log[i + 1]))
digitLogs.push_back(log);
else
letterLogs.emplace_back(log.substr(0, i), log.substr(i + 1));
}
ranges::sort(letterLogs, [](const pair<string, string>& a,
const pair<string, string>& b) {
return a.second == b.second ? a.first < b.first : a.second < b.second;
});
for (const auto& [identifier, letters] : letterLogs)
ans.push_back(identifier + ' ' + letters);
for (const string& digitLog : digitLogs)
ans.push_back(digitLog);
return ans;
}
};
/* code provided by PROGIEZ */
937. Reorder Data in Log Files LeetCode Solution in Java
class Solution {
public String[] reorderLogFiles(String[] logs) {
List<String> ans = new ArrayList<>();
List<String> digitLogs = new ArrayList<>();
List<String[]> letterLogs = new ArrayList<>();
for (final String log : logs) {
final int i = log.indexOf(' ');
if (Character.isDigit(log.charAt(i + 1)))
digitLogs.add(log);
else
letterLogs.add(new String[] {log.substring(0, i), log.substring(i + 1)});
}
Collections.sort(letterLogs, new Comparator<String[]>() {
@Override
public int compare(String[] l1, String[] l2) {
return l1[1].compareTo(l2[1]) == 0 ? l1[0].compareTo(l2[0]) : l1[1].compareTo(l2[1]);
}
});
for (String[] letterLog : letterLogs)
ans.add(letterLog[0] + " " + letterLog[1]);
for (final String digitLog : digitLogs)
ans.add(digitLog);
return ans.toArray(new String[0]);
}
}
// code provided by PROGIEZ
937. Reorder Data in Log Files LeetCode Solution in Python
class Solution:
def reorderLogFiles(self, logs: list[str]) -> list[str]:
digitLogs = []
letterLogs = []
for log in logs:
i = log.index(' ')
if log[i + 1].isdigit():
digitLogs.append(log)
else:
letterLogs.append((log[:i], log[i + 1:]))
letterLogs.sort(key=lambda x: (x[1], x[0]))
return [identifier + ' ' + letters for identifier, letters in letterLogs] + digitLogs
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.