-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
106 lines (101 loc) · 3.93 KB
/
Program.cs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestConsole
{
internal class Program
{
static void Main(string[] args)
{
//string within which to find longest pallendrome
string pallendromeNested = "anaannapaaaaaaaaaappngtymmytgnp";
var strCharArray = pallendromeNested.ToCharArray();
Array.Reverse(strCharArray);
var reversed = strCharArray.ToString();
string tmpPallendromeStr = "";
var length = pallendromeNested.Length;
var resultsKVPList = new List<KeyValuePair<int, string>>();
// The substrings can start at each index value along the length
// and traverse to the end of the length to test a relevant substring.
for (int i = 0; i < length; i++)
{
for(int j = i; j <= length; j++)
{
tmpPallendromeStr = pallendromeNested.Substring(i, j-i);
if (tmpPallendromeStr.Length >= 2)
{
if (IsPallendrome(tmpPallendromeStr))
{
resultsKVPList.
Add(new KeyValuePair<int, string>
(tmpPallendromeStr.Length, tmpPallendromeStr));
}
}
}
}
var largestPallendromeList = LargestPallendromesList(resultsKVPList);
foreach (var largestPallendrome in largestPallendromeList)
{
Console.WriteLine("Lagrest Pallendrome" + " Length: "
+ largestPallendrome.Key.ToString() + " Value: " + largestPallendrome.Value.ToString());
}
}
/// <summary>
/// Add largest palindrome value/s to list
/// this is because thaere can be two or more of the
/// same largest length
/// </summary>
/// <param name="pallendromeList">Largest palindrome/s added to this list</param>
/// <returns></returns>
private static List<KeyValuePair<int, string>> LargestPallendromesList
(List<KeyValuePair<int,string>> pallendromeList)
{
var pallendromes = new List<KeyValuePair<int,string>>();
var largestInt = 0;
foreach(var pallendrome in pallendromeList)
{
if(pallendrome.Key > largestInt)
{
largestInt = pallendrome.Key;
}
}
pallendromes = pallendromeList.Where(s=>s.Key == largestInt).ToList();
return pallendromes;
}
/// <summary>
/// Check to see if the string passed is a pallendrome
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static bool IsPallendrome(string str)
{
var strR = "";
string reversed = ReversedString(str, ref strR);
if (str.Equals(reversed))
{
return true;
}
else { return false; }
}
/// <summary>
/// I could do this with a simple reverse string
/// However at previous posting i.e. MS they required me to be able
/// to do things from scratch as much as possible.
/// For this reason I have included this block
/// </summary>
/// <param name="str"> String to reverse </param>
/// <param name="strR">Reversed String </param>
/// <returns></returns>
private static string ReversedString(string str, ref string strR)
{
var strCharArray = str.ToCharArray();
var len = strCharArray.Length;
for (int i = len - 1; i >= 0; i--)
{
strR += strCharArray[i].ToString();
}
var reversed = strR.ToString();
return reversed;
}
}
}