C++ O(NlogN) Time O(N) Space
See more code in my repo LeetCode\n\n# Correct Answer\n1. Split logs into letterLogs and digitLogs. Separate each letterLog as a pair of identifier and log cont
See more code in my repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n# Correct Answer\n1. Split `logs` into `letterLogs` and `digitLogs`. Separate each `letterLog` as a pair of identifier and log content.\n1. Sort `letterLogs`. Try comparing log contents first, if they are the same, use log identifier to break the tie.\n1. Return the concatenation of `letterLogs` and `digitLogs`.\n\n```cpp\n// OJ: https://leetcode.com/problems/reorder-data-in-log-files/\n// Author: github.com/lzl124631x\n// Time: O(NlogN)\n// Space: O(N)\nclass Solution {\npublic:\n    vector<string> reorderLogFiles(vector<string>& logs) {\n        vector<string> digitLogs, ans;\n        vector<pair<string, string>> letterLogs;\n        for (string &s : logs) {\n            int i = 0;\n            while (s[i] != \' \') ++i;\n            if (isalpha(s[i + 1])) letterLogs.emplace_back(s.substr(0, i), s.substr(i + 1));\n            else digitLogs.push_back(s);\n        }\n        sort(letterLogs.begin(), letterLogs.end(), [&](auto& a, auto& b) {\n            return a.second == b.second ? a.first < b.first : a.second < b.second;\n        });\n        for (auto &p : letterLogs) ans.push_back(p.first + " " + p.second);\n        for (string &s : digitLogs) ans.push_back(s);\n        return ans;\n    }\n};\n```\n\n---\n\n# Wrong Anwer\nThanks @ChiayuHsu for pointing out! The following code can pass the OJ, but it is actually wrong. It returns wrong answer for input `["let1 art", "let3 art ball"]` because it compares `let1` with `ball`.\n\n\n1. Separate logs into `digitLogs` and `letterLogs` according to the first character after the first space. Note that for letter logs, Each identifier is moved to the end of the string in order to facilitate sorting.\n2. Sort `letterLogs`\n3. Recover `letterLogs` so that the identifiers are moved back the the beginning of logs.\n4. Concatenate `letterLogs` with `digitLogs`, return `letterLogs`.\n\n\n```cpp\n// OJ: https://leetcode.com/problems/reorder-log-files\n// Author: github.com/lzl124631x\n// Time: O(NlogN)\n// Space: O(N)\nclass Solution {\npublic:\n    vector<string> reorderLogFiles(vector<string>& logs) {\n        vector<string> digitLogs, letterLogs;\n        for (string &s : logs) {\n            int i = 0;\n            while (s[i] != \' \') ++i;\n            if (isalpha(s[i + 1])) letterLogs.push_back(s.substr(i + 1) + " " + s.substr(0, i));\n            else digitLogs.push_back(s);\n        }\n        sort(letterLogs.begin(), letterLogs.end());\n        for (string &s : letterLogs) {\n            int i = s.size() - 1;\n            while (s[i] != \' \') --i;\n            s = s.substr(i + 1) + " " + s.substr(0, i);\n        }\n        for (string &s : digitLogs) letterLogs.push_back(s);\n        return letterLogs;\n    }\n};\n```
[]