Best Way To Find A Palindrome In A Given String

Introduction 

 
The most famous and tricky questions asked in an interview are related to a palindrome. It's common to see questions such as the following,
  • Can you check whether the given string is palindrome?
  • Can you find the longest palindrome in a substring of a given string?
  • Can you find all possible palindrome in a given string?
Everybody knows answers to these questions. Every developer can code the above problem statements. It's not like the interviewer is expecting that you cannot write code for these problems. He expects to see a solution that is fast and robust. That’s where you get an edge. There are several ways to do this and all are correct. I am showing one of them which I think is more performance-oriented and robust. So, let’s proceed with the logic...
 
Consider a string ‘ssssbbssbbss’. As you can see, the longest palindrome in the substring is ‘ssbbssbbss’
 
Now let’s create the logic.
 
Step 1
 
Convert it to charArray()
Now create a table
 
If I compare a character at location 0 with 0 it will give me true. Similarly, for 1,1 | 2,2 | 3,3, | and so on
 
Let’s fill the table,
 
 
Step 2
 
Now start comparing characters with 0 to 1, 1 to 2, 2 to 3 and so on and fill the table.
 
 
Now our basic record is generated. This will come handy later.
 
Step 3
 
Now will start comparing characters that are at least 2 indices apart and so one until first and last character and we keep filling this table until the very last column and reuse the same data.
 
If we start comparing character at index 0 with character at index 2
 
Int i = 0; Int j = 2;
i.e. if arr[i] == arr[j]
 
After this add 1 in i and subtract 1 from j. Before we start comparing, remember we already have data for this. "i" has value 1 and j has valuewhich is a palindrome. So now we iterate through step 3 to find our result. After going through the 3 steps, we can say that there is a rule:
 
Rule
 
For a string to be considered as a palindrome, all its substring should be a palindrome as well.
 
Now here's how you would implement this in code.
  1. public class Palindrome  
  2.     {  
  3.         private string value;  
  4.         private int Length = 0;  
  5.         int[,] table = null;  
  6.         char[] s = null;  
  7.         public int stringLength = 0;  
  8.         public int minCount = 0;  
  9.         public Palindrome(string palindromeString)  
  10.         {  
  11.             value = palindromeString;  
  12.             Length = value.Length;  
  13.             table = new int[Length, Length];  
  14.             s = value.ToCharArray();  
  15.             Initialize();  
  16.         }  
  17.   
  18.         private void Initialize()  
  19.         {  
  20.             for (int i = 0; i < Length; i++)  
  21.             {  
  22.                 table[i, i] = 1;  
  23.   
  24.                 if (i != Length - 1)  
  25.                 {  
  26.                     if (s[i] == s[i + 1])  
  27.                     {  
  28.                         table[i, i + 1] = 1;  
  29.                     }  
  30.                     else {  
  31.                         table[i, i + 1] = -1;  
  32.                     }  
  33.                 }  
  34.             }  
  35.         }  
  36.   
  37.         public string FindLongestPalindromInString()  
  38.         {  
  39.             for (int i = 0; i < Length; i++)  
  40.             {  
  41.                 for (int j = i+2; j < Length; j++)  
  42.                 {  

  43.                     table[i, j] = FindAllPalindrome (i, j);  
  44.                     if (table[i, j] == 1 && j-i > stringLength)  
  45.                     {  
  46.                         stringLength = j-i;  
  47.                         minCount = i;  
  48.                     }  
  49.                 }  
  50.             }  
  51.             return value.Substring(minCount, (stringLength)+1);  
  52.   
  53.         }  
  54.   
  55.         private int FindAllPalindrome(int p, int q)  
  56.         {  
  57.             int result = 0;  
  58.               
  59.             if (table[p, q] == 1)  
  60.             {  
  61.                 result = 1;  
  62.             }  
  63.             else if (table[p, q] == -1)  
  64.             {  
  65.                 result = -1;  
  66.             }  
  67.             else  
  68.             {  
  69.                 if (s[p] == s[q])  
  70.                 {  
  71.  
  72.                     result = FindAllPalindrome(p + 1, q - 1);  
  73.                 }  
  74.                 else { result = -1; }  
  75.             }  
  76.             return result;  
  77.         }  
  78.     }  
Here's the result...
 

Conclusion 

 
I hope you find it helpful. I know there are a lot of things that I can do to make this code better. Please provide suggestions below.
 
Happy reading!