patterncsharpModerate
Return the largest palindrome from the string
Viewed 0 times
thereturnlargestpalindromefromstring
Problem
Here is the question: find the largest palindrome from a string.
Ex:
Result:
I am not sure of the efficiency of the algorithm.
Ex:
ABCBAHELLOHOWRACECARAREYOUILOVEUEVOLIIAMAIDOINGGOODResult:
ILOVEUEVOLII am not sure of the efficiency of the algorithm.
static void Main(string[] args)
{
var str = "ABCBAHELLOHOWRACECARAREYOUILOVEUEVOLIIAMAIDOINGGOOD";
var longestPalindrome = GetLongestPalindrome(str);
Console.WriteLine(longestPalindrome);
Console.Read();
}
private static string GetLongestPalindrome(string input)
{
int rightIndex = 0, leftIndex = 0;
List paliList = new List();
string currentPalindrome = string.Empty;
string longestPalindrome = string.Empty;
for (int currentIndex = 1; currentIndex = 0 && rightIndex w.Length).First();
return x;
}Solution
Your basic algorithm seems pretty efficient, but building a list then sorting it just to find the longest one isn't very efficient. It would be much more efficient to just keep track of the longest palindrome:
In my tests this is 3 times faster.
I hate to break this to you, but there is a bug in your code, which will probably mean rewriting your algorithm. You algorithm assumes that the palindrome will be an odd number of characters. However, a palindrome can be an even number of characters and your code won't find it if it is.
Here's some code that will find the longest palindrome regardless:
EDIT
The above code has a bug. Here it is reworked:
private static string GetLongestPalindrome(string input)
{
int rightIndex = 0, leftIndex = 0;
var x = "";
string currentPalindrome = string.Empty;
string longestPalindrome = string.Empty;
for(int currentIndex = 1; currentIndex = 0 && rightIndex x.Length)
x = currentPalindrome;
leftIndex--;
rightIndex++;
}
}
return x;
}In my tests this is 3 times faster.
I hate to break this to you, but there is a bug in your code, which will probably mean rewriting your algorithm. You algorithm assumes that the palindrome will be an odd number of characters. However, a palindrome can be an even number of characters and your code won't find it if it is.
Here's some code that will find the longest palindrome regardless:
static string LargestPalindrome(string input)
{
string output = "";
int minimum = 2;
for(int i = 0; i minimum)
{
output = forstr;
minimum = forstr.Length;
}
}
}
return output;
}EDIT
The above code has a bug. Here it is reworked:
static string LargestPalindrome(string input)
{
int longest = 0;
int limit = input.Length;
for (int i = 0; i i; j--)
{
string forStr = input.Substring(i, j - i);
string revStr = new string(forStr.Reverse().ToArray());
if (forStr == revStr && forStr.Length > longest)
{
return forStr;
}
}
}
return "";
}Code Snippets
private static string GetLongestPalindrome(string input)
{
int rightIndex = 0, leftIndex = 0;
var x = "";
string currentPalindrome = string.Empty;
string longestPalindrome = string.Empty;
for(int currentIndex = 1; currentIndex < input.Length - 1; currentIndex++)
{
leftIndex = currentIndex - 1;
rightIndex = currentIndex + 1;
while(leftIndex >= 0 && rightIndex < input.Length)
{
if(input[leftIndex] != input[rightIndex])
{
break;
}
currentPalindrome = input.Substring(leftIndex, rightIndex - leftIndex + 1);
if(currentPalindrome.Length > x.Length)
x = currentPalindrome;
leftIndex--;
rightIndex++;
}
}
return x;
}static string LargestPalindrome(string input)
{
string output = "";
int minimum = 2;
for(int i = 0; i < input.Length - minimum; i++)
{
for(int j = i + minimum; j < input.Length - minimum; j++)
{
string forstr = input.Substring(i, j - i);
string revstr = new string(forstr.Reverse().ToArray());
if(forstr == revstr && forstr.Length > minimum)
{
output = forstr;
minimum = forstr.Length;
}
}
}
return output;
}static string LargestPalindrome(string input)
{
int longest = 0;
int limit = input.Length;
for (int i = 0; i < limit; i++)
{
for (int j = limit-1; j > i; j--)
{
string forStr = input.Substring(i, j - i);
string revStr = new string(forStr.Reverse().ToArray());
if (forStr == revStr && forStr.Length > longest)
{
return forStr;
}
}
}
return "";
}Context
StackExchange Code Review Q#43571, answer score: 11
Revisions (0)
No revisions yet.