[LeetCode] Shortest Palindrome

简介: The idea is to find the longest palindromic substring of s that begins with s[0]. Then take the remaining susbtring, reverse it and append it to the beginning of s.

The idea is to find the longest palindromic substring of s that begins with s[0]. Then take the remaining susbtring, reverse it and append it to the beginning of s.

For example, given s = "aacecaaa", the longest palindromic substring beginning with s[0] = 'a' is "aacecaa" and the remaining substring is "a". Reverse it and append it to the beginning of s gives "aaacecaaa".

For s = "abcd", the longest palindromic substring beginning with s[0] = 'a' is "a" and the remaining substring is "bcd". Reverse it and append it to the beginning of s gives "dcbabcd".

The most difficult part is to implement the Manacher's algorithm to find the longest palindromic substring starting with s[0]. Please refer to this nice article if you want to know how it works.

The code is as follows.

 1 class Solution {
 2 public:
 3     string shortestPalindrome(string s) {
 4         string t = process(s);
 5         int n = t.length(), center = 0, right = 0;
 6         int* palin = new int[n];
 7         for (int i = 1; i < n - 1; i++) {
 8             int i_mirror = 2 * center - i;
 9             palin[i] = (right > i) ? min(palin[i_mirror], right - i) : 0;
10             while (t[i + palin[i] + 1] == t[i - palin[i] - 1])
11                 palin[i]++;
12             if (i + palin[i] > right) {
13                 center = i;
14                 right = i + palin[i];
15             }
16         }
17         int pos;
18         for (int i = n - 2; i; i--) {
19             if (i - palin[i] == 1) {
20                 pos = palin[i];
21                 break;
22             }
23         }
24         string tail = s.substr(pos); 
25         reverse(tail.begin(), tail.end());
26         return tail + s;
27     }
28 private:
29     string process(string& s) {
30         int n = s.length();
31         string t(2 * n + 3, '#');
32         t[0] = '$'; t[2 * n + 2] = '%';
33         for (int i = 0; i < n; i++)
34             t[2 * (i + 1)] = s[i];
35         return t;
36     }
37 };

Note that this part of the code is just to find the ending position of the longest palindromic substring begining with s[0].

1 int pos;
2 for (int i = n - 2; i; i--) {
3     if (i - palin[i] == 1) {
4         pos = palin[i];
5         break;
6     } 
7 }

 

目录
相关文章
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
53 0
LeetCode 409. Longest Palindrome
|
索引
LeetCode 336. Palindrome Pairs
给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。
92 0
LeetCode 336. Palindrome Pairs
|
算法 索引
LeetCode 214. Shortest Palindrome
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
56 0
LeetCode 214. Shortest Palindrome
|
canal
LeetCode 125. Valid Palindrome
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
64 0
LeetCode 125. Valid Palindrome
Leetcode-Easy 234. Palindrome Linked List
Leetcode-Easy 234. Palindrome Linked List
44 0
Leetcode-Easy 234. Palindrome Linked List
【LeetCode】Palindrome Pairs(336)
  Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a   palindrome.
80 0
LeetCode之Palindrome Number(回文数)
LeetCode之Palindrome Number(回文数)
60 0
|
Java Python
LeetCode 234:回文链表 Palindrome Linked List
​请判断一个链表是否为回文链表。 Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题? Follow up:Could you do it in O(n) time and O(1) space? 解题思路: 首先是寻找链表中间节点,这个可以用快慢指针来解决,快指针速度为2,慢指针速度为1,快指针遍历完链表时,慢指针刚好走到中间节点(相对)。
659 0
|
Java
[LeetCode]Palindrome Number解析
链接:https://leetcode.com/problems/palindrome-number/#/description难度:Easy题目:9.Palindrome Number Determine whether an integer is a palindrome.
809 0