Leetcode 214 Solution

This article provides solution to leetcode question 214 (shortest-palindrome)

https://leetcode.com/problems/shortest-palindrome

Solution

class Solution {
public:
    string shortestPalindrome(string s) {
        string t = s;
        reverse(t.begin(), t.end());
        int n = s.size();
        int i = n;
        for (i = n; i >= 0; i--)
        {
            if (s.substr(0, i) == t.substr(n - i))
                break;
        }

        return t.substr(0, n - i) + s;
    }
};