<p>Given an array of strings <code>strs</code>, return <em>the length of the <strong>longest uncommon subsequence</strong> between them</em>. If the longest uncommon subsequence does not exist, return <code>-1</code>.</p>

<p>An <strong>uncommon subsequence</strong> between an array of strings is a string that is a <strong>subsequence of one string but not the others</strong>.</p>

<p>A <strong>subsequence</strong> of a string <code>s</code> is a string that can be obtained after deleting any number of characters from <code>s</code>.</p>

<ul>
	<li>For example, <code>&quot;abc&quot;</code> is a subsequence of <code>&quot;aebdc&quot;</code> because you can delete the underlined characters in <code>&quot;a<u>e</u>b<u>d</u>c&quot;</code> to get <code>&quot;abc&quot;</code>. Other subsequences of <code>&quot;aebdc&quot;</code> include <code>&quot;aebdc&quot;</code>, <code>&quot;aeb&quot;</code>, and <code>&quot;&quot;</code> (empty string).</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> strs = ["aba","cdc","eae"]
<strong>Output:</strong> 3
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> strs = ["aaa","aaa","aa"]
<strong>Output:</strong> -1
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= strs.length &lt;= 50</code></li>
	<li><code>1 &lt;= strs[i].length &lt;= 10</code></li>
	<li><code>strs[i]</code> consists of lowercase English letters.</li>
</ul>

class Solution {
public:
    int findLUSlength(vector<string>& strs) {
        int max_length = -1;
        for (int i = 0; i < strs.size(); ++i) {
            vector<int> ind(strs.size());
            for (int j = 0; j < ind.size(); ++j) {
                ind[j] = -1;
            }
            max_length = max(max_length, getUSLength(strs, i, 0, ind, 0));
        }
        return max_length;
    }

    int getUSLength(const vector<string>& strs, int i, int j, const vector<int>& ind, int curr) {
        int cnt = 0;
        for (int k = 0; k < strs.size(); ++k) {
            // cout << i << " " << j << " " << curr << " " << k << " " << ind[k] << endl;
            if (k == i) {
                continue;
            }
            if (ind[k] >= 0 && ind[k] >= strs[k].length()) {
                ++cnt;
            }
        }
        if (cnt == strs.size() - 1) {
            return curr + strs[i].length() - j;
        }
        if (j == strs[i].length()) {
            return -1;
        }
        
        // Don't take it.
        int max_length = getUSLength(strs, i, j + 1, ind, curr);

        // Take it.
        vector<int> ind_copy = ind;
        for (int k = 0; k < strs.size(); ++k) {
            if (k == i) {
                continue;
            }
            int l;
            for (l = ind_copy[k] + 1; l < strs[k].length() && strs[k][l] != strs[i][j]; ++l) {
            }
            ind_copy[k] = l;
        }
        max_length = max(max_length, getUSLength(strs, i, j + 1, ind_copy, curr + 1));
        return max_length;
    }
};
37700