[Leetcode] Shortest palindrome Shortest palindrome string _c

Source: Internet
Author: User

Given A string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome your can find by performing this transformation.

For example:

Given "Aacecaaa", Return "AAACECAAA".

Given "ABCD", Return "DCBABCD".

Credits:
Special @ifanchu for adding this problem and creating all test cases. @Freezen for additional test cases.

This problem let us seek the shortest palindrome string, Leetcode in the other questions about Palindrome string palindrome number verify palindrome numbers, Validate palindrome verify palindrome string, palindrome partitioning Split palindrome string, palindrome Partitioning II split palindrome string bis and longest palindromic Substring longest palindrome string. The title lets us add a minimum number of characters to the front of the given string s. To make it a palindrome, then we look at the two examples given in the topic, in the worst case of which there are no identical characters in S, then the minimum number of characters to be added is S.size ()-1, and the first example string contains a palindrome string, Just add one more character to the front, and one thing to note is that the string that you added earlier starts at the end of S, and a person adds it forward, so we just need to know the number of additions to the front that starts at the end of S. If this problem can not pass OJ with brute force, so we need to use some more ingenious methods to solve. Here we use the KMP algorithm, the KMP algorithm is a special use to match the string of efficient algorithm, the specific way to see this blog from beginning to end thoroughly understand KMP. We connect s with its transpose r, with a different character in the middle and a new string T, we also need an array p with the same T-length, where p[i] represents the number of the same prefix suffixes from the t[i] to the beginning of the substring, which can be explained in the KMP algorithm. Finally, we add a string of different numbers to s before the code is as follows:

Class Solution {public
:
    string Shortestpalindrome (string s) {
        string r = S;
        Reverse (R.begin (), R.end ());
        String T = s + "#" + R;
        Vector<int> P (t.size (), 0);
        for (int i = 1; i < t.size (); ++i) {
            int j = p[i-1];
            while (J > 0 && t[i]!= t[j]) j = p[j-1];
            P[i] = (j = = T[i] = = T[j]);
        }
        Return r.substr (0, S.size ()-P[t.size ()-1]) + S;
    }
};

Resources:

Https://leetcode.com/discuss/36807/c-8-ms-kmp-based-o-n-time-%26-o-n-memory-solution

http://blog.csdn.net/v_july_v/article/details/7041827

Http://www.cnblogs.com/easonliu/p/4522724.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.