<p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>

<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "aacecaaa"
<strong>Output:</strong> "aaacecaaa"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "abcd"
<strong>Output:</strong> "dcbabcd"
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>s</code> consists of lowercase English letters only.</li>
</ul>

class Solution {
public:
    string shortestPalindrome(string s) {

        if (s.size() <= 1)
            return s;
        if (s.size() == 2) {
            if (s.front() == s.back())
                return s;
            s.insert(s.begin(), s.back());
            return s;
        }

        std::reverse(s.begin(), s.end());
        // std::cout << s << std::endl;

        for (size_t i = (s.size() - 1u) / 2u; i < s.size(); ++i) {

            if (i * 2 + 1 >= s.size()) {
                for (int j = i, k = i; j >= 0 && k < int(s.size()); --j, ++k)
                    if (s[j] != s[k])
                        goto not_odd_palyndrome;

                return MakeOddPalyndrome(std::string_view(s).substr(0, i + 1));
            not_odd_palyndrome:;
            }

            for (int j = i, k = i + 1; j >= 0 && k < int(s.size()); --j, ++k)
                if (s[j] != s[k])
                    goto not_even_palyndrome;

            return MakeEvenPalyndrome(std::string_view(s).substr(0, i + 1));
        not_even_palyndrome:;
        }

        return s;
    }

    std::string MakeEvenPalyndrome(const std::string_view s) {
        std::string res;
        res.resize(s.size() * 2);
        for (size_t i = 0; i < s.size(); ++i) {
            res[i] = s[i];
            res[res.size() - 1 - i] = s[i];
        }
        return res;
    }

    std::string MakeOddPalyndrome(const std::string_view s) {
        std::string res;
        res.resize((s.size() - 1) * 2 + 1);
        for (size_t i = 0; i < s.size() - 1; ++i) {
            res[i] = s[i];
            res[res.size() - 1 - i] = s[i];
        }
        res[s.size() - 1] = s.back();
        return res;
    }
};
9200