good-article.2--------Longest Palindromic Substring Part I
来源:互联网 发布:linux服务器禁ping 编辑:程序博客网 时间:2024/09/21 06:48
This interesting problem has been featured in the famous Greplin programming challenge, and is asked quite often in the interviews. Why? Because this problem can be attacked in so many ways. There are five different solutions that I am aware of. Are you up to the challenge?
Head over to Online Judge to solve it now! (you may submit either C++ or Java solution)
Hint:
First, make sure you understand what a palindrome means. A palindrome is a string which reads the same in both directions. For example, “aba” is a palindome, “abc” is not.
A common mistake:
Some people will be tempted to come up with a quick solution, which is unfortunately flawed (however can be corrected easily):
This seemed to work, let’s see some examples below.
For example,
S = “caba”, S’ = “abac”.
The longest common substring between S and S’ is “aba”, which is the answer.
Let’s try another example:
S = “abacdfgdcaba”, S’ = “abacdgfdcaba”.
The longest common substring between S and S’ is “abacd”. Clearly, this is not a valid palindrome.
We could see that the longest common substring method fails when there exists a reversed copy of a non-palindromic substring in some other part of S. To rectify this, each time we find a longest common substring candidate, we check if the substring’s indices are the same as the reversed substring’s original indices. If it is, then we attempt to update the longest palindrome found so far; if not, we skip this and find the next candidate.
This gives us a O(N2) DP solution which uses O(N2) space (could be improved to use O(N) space). Please read more about Longest Common Substring here.
Brute force solution, O(N3):
The obvious brute force solution is to pick all possible starting and ending positions for a substring, and verify if it is a palindrome. There are a total of C(N, 2) such substrings (excluding the trivial solution where a character itself is a palindrome).
Since verifying each substring takes O(N) time, the run time complexity is O(N3).
Dynamic programming solution, O(N2) time and O(N2) space:
To improve over the brute force solution from a DP approach, first think how we can avoid unnecessary re-computation in validating palindromes. Consider the case “ababa”. If we already knew that “bab” is a palindrome, it is obvious that “ababa” must be a palindrome since the two left and right end letters are the same.
Stated more formally below:
Therefore,
The base cases are:
P[ i, i+1 ] ← ( Si = Si+1 )
This yields a straight forward DP solution, which we first initialize the one and two letters palindromes, and work our way up finding all three letters palindromes, and so on…
This gives us a run time complexity of O(N2) and uses O(N2) space to store the table.
Additional exercise:
Could you improve the above space complexity further and how?
A simpler approach, O(N2) time and O(1) space:
In fact, we could solve it in O(N2) time without any extra space.
We observe that a palindrome mirrors around its center. Therefore, a palindrome can be expanded from its center, and there are only 2N-1 such centers.
You might be asking why there are 2N-1 but not N centers? The reason is the center of a palindrome can be in between two letters. Such palindromes have even number of letters (such as “abba”) and its center are between the two ‘b’s.
Since expanding a palindrome around its center could take O(N) time, the overall complexity is O(N2).
Further Thoughts:
Does an O(N) solution exist? You bet! However, it is not trivial and requires some very clever observation. The O(N) solution is explained in my next post.
- good-article.2--------Longest Palindromic Substring Part I
- good-article.3--------Longest Palindromic Substring Part II
- Longest Palindromic Substring Part I
- 【Leetcode】Longest Palindromic Substring Part
- Longest Palindromic Substring Part II
- Longest Palindromic Substring Part II
- Longest Palindromic Substring Part II
- leetcode Longest Palindromic Substring Part II
- LeetCode Longest Palindromic Substring Part Manacher ALGORITHM
- leetcode 5 Longest Palindromic Substring(2)
- LeetCode: Longest Palindromic Substring
- LeetCode Longest Palindromic Substring
- LeetCode: Longest Palindromic Substring
- [Leetcode] Longest Palindromic Substring
- Longest Palindromic substring
- [LeetCode] Longest Palindromic Substring
- LeetCode5:Longest Palindromic Substring
- Leetcode : Longest Palindromic Substring
- java解惑之字符之谜(谜题20)
- App Store 审核指南(中文版)
- 微商前途无量,可微信电商没有出路
- 单片机平台的最小偏差圆弧插补算法
- ArrayList和LinkedList区别
- good-article.2--------Longest Palindromic Substring Part I
- Linux多线程编程
- slab分配器 很好的 参考网址(个人保存)
- JSP技术入门
- 算法导论第9章 中位数和顺序统计学
- Linux shell脚本中父子进程与变量的分析
- Protocol Buffer技术详解(语言规范)
- 开源大数据查询分析引擎现状
- 13串行化