The longest palindromic substring algorithm exists since 1975. It is different (and easier) than the longest palindromic subsequence. The idea of the substring is to return “anana”, if “bananazorro” is given. This is achieved with a 2 dimensional boolean array matrix and some strange checks like this one:
1 
if (matrix[startingIndex + 1, endingIndex1] == 1 && inputText[startingIndex] ==inputText[endingIndex]) 
After writing the solution in C# (GitHub), I somehow still had problems to visualize in my head how exactly it worked. Thus, I have decided to write it in VBA and to visualize it with Excel, in order to catch it stepbystep. And after some time, I had this “Aha” moment, when I saw the display.
So, the idea of the algorithm is the following:
 There are 3 cases:
 Single letter palindrome (worst case)
 Double letter palindrome
 More than double letter palindrome
 We write the word in a matrix nxn in Excel and we color the diagonal letters:
 Then we color the letters, which are doubled to the right of the diagonal:
 And then the interesting part starts:
 We only move to the right of the yellow diagonal line, completely ignoring the left side
 We loop from 3 to the length of the word (as far as palindromes with size 1 or 2 are already checked in the previous steps)
 We check for every position, whether the first and the last letter is the same and whether the left diagonal is loop
 If true, we color in yellow as well. In the picture below, nnabann is considered palindrome, because the two ns are equal and the n to the right has a yellow predecessor in the lower left cell:

 Once we find a cell which answers our conditions, we highlight it in yellow. We also write down the current length as a maximum one (because it is ascending from 3 to the length of the word.
 At the end, we highlight the word, which is a palndrome, on a separate line:
It is really better to take a look at the code working a few times, in order to understand how it works. The letters in red in line 23 are the letters which are currently compared. The selected cell is the one which is compared to the initial diagonal line:
Here comes the VBA code (GitHub). The only requirement before pasting it in a module, is to name the worksheet with a codename tblMatrix :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 
Option Explicit Sub Main() Dim theWord As String: theWord = "kkbkannabannavannabb" Dim length As Long: length = Len(theWord) Dim maxLength As Long: maxLength = 1 Dim startAt As Long: startAt = 1 ClearTable EditTheTable length WriteTheWord theWord ReDim matrix(length  1, length  1) As Long 'For 1: Dim i As Long For i = LBound(matrix) To UBound(matrix) tblMatrix.Cells(i + 1, i + 1).Interior.Color = vbYellow Next 'For 2: For i = LBound(matrix) + 1 To UBound(matrix)  1 If (Mid(theWord, i, 1) = Mid(theWord, i + 1, 1)) Then maxLength = 2 startAt = i tblMatrix.Cells(i, i + 1).Interior.Color = vbYellow End If Next 'For >2: Dim k As Long For k = 3 To length Dim startingIndex As Long For startingIndex = 1 To length  k + 1 Dim endingIndex As Long: endingIndex = startingIndex + k  1 With tblMatrix .Cells(length + 3, 1) = Mid(theWord, startingIndex, 1) .Cells(length + 3, 2) = Mid(theWord, endingIndex, 1) .Cells(length + 3, 1).Interior.Color = vbRed .Cells(length + 3, 2).Interior.Color = vbRed End With tblMatrix.Cells(startingIndex, endingIndex).Select If (tblMatrix.Cells(startingIndex + 1, endingIndex  1).Interior.Color = vbYellow And _ Mid(theWord, startingIndex, 1) = Mid(theWord, endingIndex, 1)) Then tblMatrix.Cells(startingIndex, endingIndex).Interior.Color = vbYellow maxLength = k startAt = startingIndex End If Next startingIndex Next k With tblMatrix .Range(.Cells(length + 2, startAt), .Cells(length + 2, startAt + maxLength  1)).Interior.Color = vbYellow End With End Sub Sub EditTheTable(length As Long) tblMatrix.Cells.Delete Dim i As Long For i = 1 To length tblMatrix.Columns(i).ColumnWidth = 3.14 Next End Sub Sub ClearTable() tblMatrix.Cells.Clear End Sub Sub WriteTheWord(theWord As String) Dim row As Long Dim col As Long Dim sizeCounter As Long For row = 1 To Len(theWord) + 2 If row <> Len(theWord) + 1 Then For col = 1 To Len(theWord) sizeCounter = sizeCounter + 1 tblMatrix.Cells(row, col) = Mid(theWord, sizeCounter, 1) Next End If sizeCounter = 0 Next End Sub 
That’s all folks! Sometimes VBA is better than C#, depending on what your target is. 🙂