例如字符串“abaccddccefe”中的“ccddcc”
我想到了一个解决方案,但它运行时间为O(n^2)
算法1:
步骤:
这是一种蛮力方法
- 有 2 个 for 循环
对于 i = 1 到 i 小于 array.length -1
对于 j=i+1 到 j 小于 array.length 通过
- 这种方式,您可以从数组中获取每个可能组合的子字符串
- 有一个回文函数,用于检查字符串是否为回文,
- 因此对于每个子字符串 (i,j) 调用此函数,如果它是回文,则将其存储在字符串变量中。
- 如果找到下一个回文子串,并且它大于当前的子串,则将其替换为当前的回文子串。
- 最后你的字符串变量将有
问题的答案:
1. 该算法运行时间为 O(n^2)。
算法 2:
- 反转字符串并将其存储在不同的数组中
- 现在找到两个数组之间最大的匹配子字符串
- 但这也需要 O(n^2) 时间运行
你们能想到一个运行时间更好的算法吗? 如果可能的话 O(n) 时间
e.g "ccddcc" in the string "abaccddccefe"
I thought of a solution but it runs in O(n^2) time
Algo 1:
Steps:
Its a brute force method
- Have 2 for loops
for i = 1 to i less than array.length -1
for j=i+1 to j less than array.length
- This way you can get substring of every possible combination from the array
- Have a palindrome function which checks if a string is palindrome
- so for every substring (i,j) call this function, if it is a palindrome store it in a string variable
- If you find next palindrome substring and if it is greater than the current one, replace it with current one.
- Finally your string variable will have the answer
Issues:
1. This algo runs in O(n^2) time.
Algo 2:
- Reverse the string and store it in diferent array
- Now find the largest matching substring between both the array
- But this too runs in O(n^2) time
Can you guys think of an algo which runs in a better time. If possible O(n) time
发布评论
评论(23)
您可以使用 Manacher 算法 在
O(n)时间! 它的实现可以在此处和此处。
对于输入
String s = "HYTBCABADEFGHABCDEDCBAGHTFYW1234567887654321ZWETYGDE"
,它会找到正确的输出,即1234567887654321
。You can find the the longest palindrome using Manacher's Algorithm in
O(n)
time! Its implementation can be found here and here.For input
String s = "HYTBCABADEFGHABCDEDCBAGHTFYW1234567887654321ZWETYGDE"
it finds the correct output which is1234567887654321
.Algo 2 可能不适用于所有字符串。 以下是此类字符串“ABCDEFCBA”的示例。
并不是该字符串具有“ABC”和“CBA”作为其子字符串。 如果反转原始字符串,它将是“ABCFEDCBA”。 最长的匹配子串是“ABC”,它不是回文。
您可能还需要额外检查这个最长匹配子串是否实际上是一个运行时间为 O(n^3) 的回文。
The Algo 2 may not work for all string. Here is an example of such a string "ABCDEFCBA".
Not that the string has "ABC" and "CBA" as its substring. If you reverse the original string, it will be "ABCFEDCBA". and the longest matching substring is "ABC" which is not a palindrome.
You may need to additionally check if this longest matching substring is actually a palindrome which has the running time of O(n^3).
据我对这个问题的理解,我们可以在中心索引周围找到回文,并将我们的搜索扩展到中心的右侧和左侧。 考虑到这一点并且知道输入的角上没有回文,我们可以将边界设置为 1 且长度为 1。 在注意字符串的最小和最大边界的同时,我们验证每个中心位置对称索引(右侧和左侧)位置的字符是否相同,直到达到最大上限中心。
外部循环为 O(n)(最多 n-2 次迭代),内部 while 循环为 O(n)(最多 (n / 2) - 1 次迭代)
这是我使用其他用户提供的示例的 Java 实现。
其输出如下:
As far as I understood the problem, we can find palindromes around a center index and span our search both ways, to the right and left of the center. Given that and knowing there's no palindrome on the corners of the input, we can set the boundaries to 1 and length-1. While paying attention to the minimum and maximum boundaries of the String, we verify if the characters at the positions of the symmetrical indexes (right and left) are the same for each central position till we reach our max upper bound center.
The outer loop is O(n) (max n-2 iterations), and the inner while loop is O(n) (max around (n / 2) - 1 iterations)
Here's my Java implementation using the example provided by other users.
The output of this is the following:
使用正则表达式和 ruby,您可以扫描短回文,如下所示:
with regex and ruby you can scan for short palindromes like this:
您好,这是我的代码,用于查找字符串中最长的回文。
请参考以下链接了解算法http://stevekrenzel.com/articles/longest-palnidrome
使用的测试数据为 HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE
Hi Here is my code to find the longest palindrome in the string.
Kindly refer to the following link to understand the algorithm http://stevekrenzel.com/articles/longest-palnidrome
Test data used is HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE
出于好奇、简单且不言自明的 HTH,我编写了以下 Java 程序。 谢谢。
I have written the following Java program out of curiosity, simple and self-explanatory HTH. Thanks.
最近有人问我这个问题。 这是我[最终]想出的解决方案。 我用 JavaScript 来做,因为用那种语言来说它非常简单。
基本概念是遍历字符串寻找可能的最小多字符回文(两个或三个字符的回文)。 一旦完成,扩大两侧的边界,直到它不再是回文。 如果该长度比当前最长的长度长,则将其存储并继续移动。
这肯定可以进一步清理和优化,但除了最坏的情况(同一字母的字符串)之外,它在所有情况下都应该具有相当好的性能。
I was asked this question recently. Here's the solution I [eventually] came up with. I did it in JavaScript because it's pretty straightforward in that language.
The basic concept is that you walk the string looking for the smallest multi-character palindrome possible (either a two or three character one). Once you have that, expand the borders on both sides until it stops being a palindrome. If that length is longer than current longest one, store it and move along.
This could definitely be cleaned up and optimized a little more, but it should have pretty good performance in all but the worst case scenario (a string of the same letter).
请参阅有关此主题的维基百科文章。 线性 O(n) 的 Manacher 算法 Java 实现示例解决方案来自下面的文章:
See Wikipedia article on this topic. Sample Manacher's Algorithm Java implementation for linear O(n) solution from the article below:
一种有效的
Regexp
解决方案,避免暴力破解从整个字符串长度开始,向下工作到2个字符,一旦匹配就存在
对于
“abaccddccefe”
正则表达式测试返回 ccddcc 之前有 7 个匹配项。vbs
vba
功能
An efficient
Regexp
solution which avoids brute forceStarts with the entire string length and works downwards to 2 characters, exists as soon as a match is made
For
"abaccddccefe"
the regexp tests 7 matches before returningccddcc
.vbs
vba
function
尝试字符串 -“HYTBCABADEFGHABCDEDCBAGHTFYW123456789987654321ZWETYGDE”;
它应该适用于偶数和奇数朋友。 非常感谢莫希特!
使用命名空间 std;
Try the string - "HYTBCABADEFGHABCDEDCBAGHTFYW123456789987654321ZWETYGDE";
It should work for even and odd pals. Much Thanks to Mohit!
using namespace std;
以下代码计算偶数长度和奇数长度字符串的 Palidrom。
不是最好的解决方案,但适用于两种情况
HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE
HYTBCABADEFGHABCDEDCBAGHTFYW1234567887654321ZWETYGDE
Following code calculates Palidrom for even length and odd length strings.
Not the best solution but works for both the cases
HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE
HYTBCABADEFGHABCDEDCBAGHTFYW1234567887654321ZWETYGDE
我们可以使用它找到所有长度的所有回文。
示例:
word = abcdcbc
moddedString = a#b#c#d#c#b#c
palinCount = 1010105010301
最长回文长度 = 5;
最长回文 = bcdcb
public class MyLongestPalindrome {
}
We can find all palindromes of all length using this.
Sample :
word = abcdcbc
modifiedString = a#b#c#d#c#b#c
palinCount = 1010105010301
length of longest palindrome = 5;
longest palindrome = bcdcb
public class MyLongestPalindrome {
}
这将从给定字符串返回最长的回文字符串
== 输出 ===
输入:abcccde 输出:ccc
输入:abcccbd 输出:bcccb
输入:abedccde 输出:edccde
输入:abcccdeed 输出:deed
输入:abcccbadeed 输出:abcccba
This will return longest palindrome string from given string
== OUTPUT ===
Input: abcccde Output: ccc
Input: abcccbd Output: bcccb
Input: abedccde Output: edccde
Input: abcccdeed Output: deed
Input: abcccbadeed Output: abcccba
这是 JavaScript 中的一个实现:
Here's an implementation in javascript:
对于线性求解,可以使用 Manacher 算法。 还有另一种算法称为 Gusfield 算法,下面是 java 中的代码:
您可以从 我自己的博客。
For linear solution, you can use Manacher's algorithm. There is another algorithm call Gusfield's Algorithm, and below is the code in java:
You can find more on other solutions such as the best O(n^2) solution or Manacher's algorithm from my own blog.
这里我写了一个逻辑试试:)
Here i have written a logic try it :)
该解决方案的复杂度为 O(n^2)。 O(1) 是空间复杂度。
This Solution is of O(n^2) complexity. O(1) is the space complexity.
{“DED”:3,“123456789987654321”:18,“67899876”:8,“ABCDEDCBA”:9,“456789987654”:12,“34567899876543”:14,“BCDEDCB”:7,“ABA”:3, ' 5678998765':10,'2345678998765432':16,'CDEDC':5,'789987':6,'8998':4}
('123456789987654321', 18)
{'DED': 3, '123456789987654321': 18, '67899876': 8, 'ABCDEDCBA': 9, '456789987654': 12, '34567899876543': 14, 'BCDEDCB': 7, 'ABA': 3, '5678998765': 10, '2345678998765432': 16, 'CDEDC': 5, '789987': 6, '8998': 4}
('123456789987654321', 18)
编程从给定字符串中查找最长的回文子串。
Program to find the longest substring which is palindrome from a given string.
这是我的算法:
1)将当前中心设置为第一个字母
2)同时向左和向右扩展,直到找到当前中心周围的最大回文
3)如果找到的回文比之前的回文大,则更新4
) 将当前中心设置为下一个字母
5) 对字符串中的所有字母重复步骤 2) 到 4)
这在 O(n) 中运行。
希望能帮助到你。
Here is my algorithm:
1) set the current center to be the first letter
2) simultaneously expand to the left and right until you find the maximum palindrome around the current center
3) if the palindrome you find is bigger than the previous palindrome, update it
4) set the current center to be the next letter
5) repeat step 2) to 4) for all letters in the string
This runs in O(n).
Hope it helps.
参考:Wikipedia.com
我发现的最好的算法,复杂度为 O(N)
Reference: Wikipedia.com
The best algorithm i have ever found, with complexity O(N)
我的解决方案是:
my solution is :