<p>You are given an array of <code>logs</code>. Each log is a space-delimited string of words, where the first word is the <strong>identifier</strong>.</p>

<p>There are two types of logs:</p>

<ul>
	<li><b>Letter-logs</b>: All words (except the identifier) consist of lowercase English letters.</li>
	<li><strong>Digit-logs</strong>: All words (except the identifier) consist of digits.</li>
</ul>

<p>Reorder these logs so that:</p>

<ol>
	<li>The <strong>letter-logs</strong> come before all <strong>digit-logs</strong>.</li>
	<li>The <strong>letter-logs</strong> are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.</li>
	<li>The <strong>digit-logs</strong> maintain their relative ordering.</li>
</ol>

<p>Return <em>the final order of the logs</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> logs = [&quot;dig1 8 1 5 1&quot;,&quot;let1 art can&quot;,&quot;dig2 3 6&quot;,&quot;let2 own kit dig&quot;,&quot;let3 art zero&quot;]
<strong>Output:</strong> [&quot;let1 art can&quot;,&quot;let3 art zero&quot;,&quot;let2 own kit dig&quot;,&quot;dig1 8 1 5 1&quot;,&quot;dig2 3 6&quot;]
<strong>Explanation:</strong>
The letter-log contents are all different, so their ordering is &quot;art can&quot;, &quot;art zero&quot;, &quot;own kit dig&quot;.
The digit-logs have a relative order of &quot;dig1 8 1 5 1&quot;, &quot;dig2 3 6&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> logs = [&quot;a1 9 2 3 1&quot;,&quot;g1 act car&quot;,&quot;zo4 4 7&quot;,&quot;ab1 off key dog&quot;,&quot;a8 act zoo&quot;]
<strong>Output:</strong> [&quot;g1 act car&quot;,&quot;a8 act zoo&quot;,&quot;ab1 off key dog&quot;,&quot;a1 9 2 3 1&quot;,&quot;zo4 4 7&quot;]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= logs.length &lt;= 100</code></li>
	<li><code>3 &lt;= logs[i].length &lt;= 100</code></li>
	<li>All the tokens of <code>logs[i]</code> are separated by a <strong>single</strong> space.</li>
	<li><code>logs[i]</code> is guaranteed to have an identifier and at least one word after the identifier.</li>
</ul>

// Idea: straightforward implementation
// achieved the zero-copy with move semantics and string_view.
class Solution {
    static bool isDigitLog(const string& s) {
        bool foundFirstSpace = false;
        for (char c : s) {
            if (isspace(c)) {
                foundFirstSpace = true;
            }
            // after the first space, the remaining chars
            // should be either digits or spaces.
            if (foundFirstSpace) {
                if (!isdigit(c) && !isspace(c)) {
                    return false;
                }
            }
        }
        return true;
    }

public:
    vector<string> reorderLogFiles(vector<string>& logs) {
        vector<string> letters, digits;
        for (auto& x : logs) {
            if (isDigitLog(x)) {
                digits.push_back(std::move(x));
            } else {
                letters.push_back(std::move(x));
            }
        }
        sort(letters.begin(), letters.end(), [](const auto& a, const auto& b) {
            // get the string_views of the log id and content.
            // this avoids any copying of the string chars
            auto idxA = a.find(' ');
            string_view idA = string_view(&a[0], idxA);
            string_view contentA =
                string_view(&a[idxA + 1], a.size() - idxA - 1);

            auto idxB = b.find(' ');
            string_view idB = string_view(&b[0], idxB);
            string_view contentB =
                string_view(&b[idxB + 1], b.size() - idxB - 1);

            return contentA < contentB || contentA == contentB && idA < idB;
        });

        // cout << "letters are: " << endl;
        // for (const auto& s : letters) {
        //     cout << s << ", ";
        // }
        // cout << endl;
        // cout << "digits are: " << endl;
        // for (auto& s : digits) {
        //     cout << s << ", ";
        // }
        // cout << endl;

        letters.insert(letters.end(), std::make_move_iterator(digits.begin()),
                       std::make_move_iterator(digits.end()));
        return letters;
    }
};
12252