Skip to content

Instantly share code, notes, and snippets.

@wesleyduff
Last active March 6, 2018 17:36
Show Gist options
  • Save wesleyduff/972479581ed8f0796e66f3dab0dbef78 to your computer and use it in GitHub Desktop.
Save wesleyduff/972479581ed8f0796e66f3dab0dbef78 to your computer and use it in GitHub Desktop.
Longest Palindrome : C# : Working Example : https://dotnetfiddle.net/5lq3tQ
using System;
namespace PalindromeTest {
/* -------
* Table - store palindromes : Multi Dimensional Array
* =========
* Checks for palindrom
* - Each letter will be a palindrom
* - - Set this as "True" in our Table AND set the new longest palindrome
* - Letters that are the same and are right next to eachother " i & i + 1 "
* - - Set this as "True" in our Table AND set the new longest palindrome
* - First and Last letter are the same AND Table[(i +1), (j -1)] is "True" in our table
* - - Set this as "True" in our Table AND set the new longest palindrome
* -------
*/
public interface IStringUtilities {
String GetSubstring(String s, int start, int end);
}
public class Utilities : IStringUtilities {
public String GetSubstring(String s, int start, int end){
int length = (end - start) + 1;
if(end >= s.Length){
return s.Substring(start, (s.Length -1));
} else {
return s.Substring(start, length);
}
}
}
public abstract class StringExtensions : IStringUtilities {
private readonly IStringUtilities _u;
public StringExtensions(IStringUtilities u){
_u = u;
}
public String GetSubstring(String s, int start, int end){
return _u.GetSubstring(s, start, end);
}
}
public class Palindrome : StringExtensions {
public String chars;
public int length;
public bool[,] table;
public String longest_palindrome;
public int palindrome_length;
public Palindrome(String val) : base(new Utilities())
{
this.chars = val;
this.length = val.Length;
this.table = new bool[this.length, this.length];
this.longest_palindrome = "";
this.palindrome_length = this.longest_palindrome.Length;
}
public void CheckAndSetPalidromeLevel1(int i){
table[i, i] = true;
if( GetSubstring(chars, i, i).Length > palindrome_length ){
longest_palindrome = GetSubstring(chars, i, i);
}
}
public void CheckAndSetPalindromeLevel2(int i){
if(chars[i] == chars[i + 1]){
table[i, (i+1)] = true;
if(GetSubstring(chars, i, (i + 1)).Length > length ){
longest_palindrome = GetSubstring(chars, i, (i + 1));
}
}
}
public void CheckAndSetPalindromeGreaterThan2(int k, int i){
//do not allow "j" to be larger than or equal to palindromString.Length
int j = 0;
if((i + k) < length){
j = i + k;
} else {
j = length -1;
}
Console.WriteLine("----- test ----- \n [i, j]: [{0}, {1}], \n palindromString[i]: {2}, \n palindromString[j]: {3}, \n String : {4} \n Table : {5}", i, j, chars[i], chars[j], GetSubstring(chars, i, j), table[(i + 1),(j - 1)]);
if(chars[i] == chars[j] && table[(i + 1),(j - 1)] == true){
if(GetSubstring(chars, i, j).Length > palindrome_length){
//Set new palindrome
Console.WriteLine("---- NEW LONGER ----{0}", GetSubstring(chars, i, j));
longest_palindrome = GetSubstring(chars, i, j);
}
table[i, j] = true;
}
}
}
public class Program
{
public static void Main()
{
Palindrome palindrome = new Palindrome("abaxabaxabb");
//Set all cells to "false" inside our table to hold values
for(int i = 0; i < palindrome.length; i++){
for(int k = 0; k < palindrome.length; k++){
palindrome.table[i,k] = false;
}
}
Console.WriteLine("----- 1");
for(int i = 0; i < palindrome.length; i++)
{
Console.WriteLine("value: {0}, k: [{1}, {1}]", palindrome.table[i, i], i);
palindrome.CheckAndSetPalidromeLevel1(i);
}
Console.WriteLine("----- 2");
//check to see if two characters side by side are equal
for(int i = 0; i < palindrome.length - 1; i++){
Console.WriteLine("value: {0}, k: [{1}, {2}]", palindrome.table[i, (i + 1)], i, (i + 1));
palindrome.CheckAndSetPalindromeLevel2(i);
}
Console.WriteLine("----- > 2");
for(int k = 2; k < palindrome.length - 2; k++){
for(int i = 0; i < palindrome.length - 2; i++){
palindrome.CheckAndSetPalindromeGreaterThan2(k, i);
}
}
// ---------- DONE --------- Write out results -----------
Console.WriteLine("==================== \n ");
Console.WriteLine("Longest Palindrome is : {0}", palindrome.longest_palindrome);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment