Two sorts one partition

First we partition by the first character of the first space.
then we sort up to the partition boundary
then we sort by the words after the identifier
cpp

class Solution {
public:
    vector<string> reorderLogFiles(vector<string>& logs) {
        auto it = stable_partition(logs.begin(), logs.end(), [](auto const& a){return a[a.find(' ')+1] >= 'a' && a[a.find(' ')+1] <= 'z';});
        sort(logs.begin(), it);
        stable_sort(logs.begin(), it, [](auto const& a, auto const& b){return a.substr(a.find(' ')+1) < b.substr(b.find(' ')+1);});
        retur
n logs;
    }
};

notice that python does not have a partition function to so we need to use a different technique.
we instead use three sorts.
python

class Solution:
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        logs.sort(key = lambda x: 0 if 'z' >= x[x.index(' ')+1] >= 'a' else 1)
        logs.sort(key = lambda x: x if 'z'>= x[x.index(' ')+1] >= 'a' else '~')
        logs.sort(key = lambda x: x[x.find(' ')+1:] if 'z'>= x[x.index(' ')+1] >= 'a' else '~')
        return logs
Comments (0)