<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>

class Solution {
public:

    // logs = ["dig1 8 1 5 1","let1 art can", "dig2 3 6","let2 own kit dig","let3 art zero"]
    // ["let1 art can", "let3 art zero", "let2 own kit dig", "dig1 8 1 5 1", "dig2 3 6", ]

    // Put all letter logs at the start of the of list
    // Place the identifier of all letter logs at the back of the logs
    // Sort the letter logs lexicographically (by contents first)
    // Place the identifier of all letter logs back in place

    // ["dig1 8 1 5 1","let1 art can", "dig2 3 6","let2 own kit dig","let3 art zero"]
    // ["dig1 8 1 5 1","let1 art can", "let3 art zero", ,"let2 own kit dig", "dig2 3 6"]

    // ["let1 art can", "dig1 8 1 5 1", "let2 own kit dig","let3 art zero"]

    // Given there are n total logs and k letter logs
    // O(n) time to move all letter logs to the front
    // O(k) time to put identifier at end of letter logs
    // O(klogk) time to sort all letter logs
    // O(k) time to put identifier at front of letter logs

    // O(n + klogk) runtime with O(1) space

    static bool compareLogs(const string&a, const string&b) {

            int space_idx_a = a.find(' ');
            string content_a = a.substr(space_idx_a + 1, a.size() - (space_idx_a+ 1));
            string id_a = a.substr(0, space_idx_a);

            int space_idx_b = b.find(' ');
            string content_b = b.substr(space_idx_b + 1, b.size() - (space_idx_b+ 1));
            string id_b = b.substr(0, space_idx_b);

            if (content_a == content_b) {
                return id_a < id_b;
            }
            return content_a < content_b;
    }

    vector<string> reorderLogFiles(vector<string>& logs) {
        // Move all digit logs to the back of logs
        int n = logs.size();
        int digi_idx = n-1;
        for (int i = n-1; i >= 0; i--) {
            if (!isLetterLog(logs[i])) {
                swap(logs[i], logs[digi_idx]);
                digi_idx--;
            }
        }

        // Add the identifier to the back of all letter logs
        int num_letter_logs = 0;
        for (int i = 0; i < n; i++) {
            if (isLetterLog(logs[i])) {
                num_letter_logs++;
            } else {
                break;
            }
        }

        // Sort all letter logs
        sort(logs.begin(), logs.begin() + num_letter_logs, compareLogs);

        return logs;

    }

    bool isLetterLog(string log) {
        char content_char = log[log.find(' ')+1];
        return !(content_char >= '0' && content_char <= '9');
    }
};
16452