<p>Given two strings: <code>s1</code> and <code>s2</code> with the same&nbsp;size, check if some&nbsp;permutation of string <code>s1</code> can break&nbsp;some&nbsp;permutation of string <code>s2</code> or vice-versa. In other words <code>s2</code> can break <code>s1</code>&nbsp;or vice-versa.</p>

<p>A string <code>x</code>&nbsp;can break&nbsp;string <code>y</code>&nbsp;(both of size <code>n</code>) if <code>x[i] &gt;= y[i]</code>&nbsp;(in alphabetical order)&nbsp;for all <code>i</code>&nbsp;between <code>0</code> and <code>n-1</code>.</p>

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

<pre>
<strong>Input:</strong> s1 = &quot;abc&quot;, s2 = &quot;xya&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> &quot;ayx&quot; is a permutation of s2=&quot;xya&quot; which can break to string &quot;abc&quot; which is a permutation of s1=&quot;abc&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;abe&quot;, s2 = &quot;acd&quot;
<strong>Output:</strong> false 
<strong>Explanation:</strong> All permutations for s1=&quot;abe&quot; are: &quot;abe&quot;, &quot;aeb&quot;, &quot;bae&quot;, &quot;bea&quot;, &quot;eab&quot; and &quot;eba&quot; and all permutation for s2=&quot;acd&quot; are: &quot;acd&quot;, &quot;adc&quot;, &quot;cad&quot;, &quot;cda&quot;, &quot;dac&quot; and &quot;dca&quot;. However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;leetcodee&quot;, s2 = &quot;interview&quot;
<strong>Output:</strong> true
</pre>

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

<ul>
	<li><code>s1.length == n</code></li>
	<li><code>s2.length == n</code></li>
	<li><code>1 &lt;= n &lt;= 10^5</code></li>
	<li>All strings consist of lowercase English letters.</li>
</ul>

class Solution {
public:
    bool checkIfCanBreak(string s1, string s2)
    {
        int i, j, l, m, *w1, *w2, *z1, *z2;

        l = s1.length();
        w1 = new int[26];
        std::fill(&w1[0], &w1[26], 0);
        w2 = new int[26];
        std::fill(&w2[0], &w2[26], 0);
        for (i = 0; i < l; i++) {
            w1[s1[i] - 'a']++;
            w2[s2[i] - 'a']++;
        }
        z1 = new int[26];
        std::copy(&w1[0], &w1[26], &z1[0]);
        z2 = new int[26];
        std::copy(&w2[0], &w2[26], &z2[0]);
        m = l;
        i = j = 0;
        while (i < 26 && z1[i] == 0)
            i++;
        while (j < 26 && z2[j] == 0)
            j++;
        while (j < 26 && i <= j) {
            z1[i]--;
            z2[j]--;
            m--;
            if (m == 0)
                break;
            while (i < 26 && z1[i] == 0)
                i++;
            while (j < 26 && z2[j] == 0)
                j++;
        }
        if (m > 0) {
            std::copy(&w1[0], &w1[26], &z1[0]);
            std::copy(&w2[0], &w2[26], &z2[0]);
            m = l;
            i = j = 0;
            while (i < 26 && z1[i] == 0)
                i++;
            while (j < 26 && z2[j] == 0)
                j++;
            while (i < 26 && i >= j) {
                z1[i]--;
                z2[j]--;
                m--;
                if (m == 0)
                    break;
                while (i < 26 && z1[i] == 0)
                    i++;
                while (j < 26 && z2[j] == 0)
                    j++;
            }
        }
        delete[] z1;
        delete[] z2;
        delete[] w1;
        delete[] w2;

        return m == 0;
    }
};
13300