-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathLongestPalindromicAlgorithm.cs
More file actions
46 lines (43 loc) · 1.29 KB
/
LongestPalindromicAlgorithm.cs
File metadata and controls
46 lines (43 loc) · 1.29 KB
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
using System;
class Startup
{
static void Main()
{
string inputText = "kkbkannabannavannabb";
int maxLength = 1;
int length = inputText.Length;
int[,] matrix = new int[length,length];
int startAt = 1;
//for 1:
for (int i = 0; i < length; i++)
{
matrix[i, i] = 1;
}
//for 2:
for (int i = 0; i < length-1; i++)
{
if (inputText[i]==inputText[i+1])
{
maxLength = 2;
startAt = i;
matrix[i,i+1] = 1;
}
}
//for >2:
for (int k = 3; k <= length; k++)
{
for (int startingIndex = 0; startingIndex < length-k+1; startingIndex++)
{
int endingIndex = startingIndex + k - 1;
if (matrix[startingIndex + 1, endingIndex-1] == 1 && inputText[startingIndex] ==inputText[endingIndex])
{
Console.WriteLine(startingIndex+1 +" " + inputText[startingIndex]);
matrix[startingIndex, endingIndex] = 1;
maxLength = k;
startAt = startingIndex;
}
}
}
Console.WriteLine(inputText.Substring(startAt,maxLength));
}
}