Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
2 | Add Two Numbers | Medium | Linked List, Math | save a dummy head for output
3 | Longest Substring Without Repeating Characters | Medium | Hash Table, Two Pointers, String | update the max length when traversing with the right pointer, move left pointer to ensure non-repeating
4 | Median of Two Sorted Arrays | Hard | Binary Search | find the largest numbers on the left halves and smallest numbers on the right halves in each array, then use the formula median = 0.5 * (max(L1, L2) + min(R1, R2))
5 | Longest Palindromic Substring | Medium | String | start from the middle and check both sides
7 | Reverse Integer | Easy | Math | test overflow
8 | String to Integer (atoi) | Easy | String, Math | remove leading white spaces, test overflow
15 | 3Sum | Medium | Two Pointers | sort first, convert into two sum problem then approach from both sides
Expand All @@ -19,6 +20,7 @@
76 | Minimum Window Substring | Hard | Hash Table, Two Pointers, String | build hash table, move right pointer to match, move left pointer to find minimum
155 | Min Stack | Easy | Stack, Design | use two stacks to implement, keep the current minimum on the top
167 | Two Sum II - Input array is sorted | Medium | Two Pointers | approach from both sides
214 | Shortest Palindrome | Hard | [KMP Algorithm](http://blog.csdn.net/v_july_v/article/details/7041827), String | build a string with its reverse, then use KMP to find the longest common prefix and suffix, which is the longest palindrome, the length will be shown as the last value in the KMP table
239 | Sliding Window Maximum | Hard | Queue | use a double-ended queue (deque) to store the potential maximum
306 | Additive Number | Medium | String, Math | handle leading zero cases and recursively test if it is additive
371 | Sum of Two Integers | Easy | [Bit Manipulation](https://discuss.leetcode.com/topic/50315/a-summary-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently) | use XOR to handle '0 + 0', '0 + 1', '1 + 0', use AND then left shift to handle '1 + 1'
Expand Down
79 changes: 79 additions & 0 deletions cpp/214_Shortest_Palindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 214. Shortest Palindrome
/**
* 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
* you can find by performing this transformation.
*
* For example:
*
* Given "aacecaaa", return "aaacecaaa".
*
* Given "abcd", return "dcbabcd".
*
* Tags: String
*
* Similar Problems: (M) Longest Palindromic Substring, (E) Implement strStr(), (H) Palindrome Pairs
*
* Author: Kuang Qin
*/

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

class Solution {
public:
string shortestPalindrome(string s) {
if (s.size() < 2) {
return s;
}

// build a string with its reverse
// then use KMP to find the longest common prefix and suffix, which is a palindrome
//
// for example:
// a b a b c # c b a b a
//
// longest common string in prefix and suffix is "aba"
// its length will be shown as the value of the last char in the KMP table

string r = s;
reverse(r.begin(), r.end());
string l = s + "#" + r;

int n = l.size();
vector<int> table(n, 0); // KMP lookup table
for (int i = 1; i < n; i++) {
// in substring l[0...i]: l[i] is the suffix, l[j] should be set to the char after the longest common prefix
// table[i - 1] shows the longest common prefix and suffix matched for the last char
int j = table[i - 1];

// if not match, recursively searching for a shorter string
while (j > 0 && l[i] != l[j]) {
j = table[j - 1];
}

// if matched, extend the common prefix and suffix by 1
if (l[j] == l[i]) {
j++;
}

// assign the value to current substring
table[i] = j;
}

// table[n - 1] represents the length of the longest palindrome
return r.substr(0, s.size() - table[n - 1]) + s;
}
};

int main() {
string s = "ababc";
Solution sol;
string ans = sol.shortestPalindrome(s);
cout << ans << endl;
cin.get();

return 0;
}