Skip to content

Commit

Permalink
Add LongestPalindromicSubstring in C# (TheRenegadeCoder#3989)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaslov007 authored Nov 8, 2024
1 parent 7c09559 commit 4ce60e7
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions archive/c/c-sharp/LongestPalindromicSubstring.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Text.RegularExpressions;

namespace SamplePrograms
{
public class LongestPalindromicSubstring
{
public static void Main(string[] args)
{
string input = string.Join(" ", args);
Console.WriteLine(LongestPalindrome(input));
}

public static string LongestPalindrome(string input)
{
if (string.IsNullOrEmpty(input) || !ContainsPalindrome(input))
{
return "Usage: please provide a string that contains at least one palindrome";
}

int start = 0;
int end = 0;

for (int i = 0; i < input.Length; i++)
{
int lengthOne = ExpandAroundCenter(input, i, i);
int lengthTwo = ExpandAroundCenter(input, i, i + 1);
int length = Math.Max(lengthOne, lengthTwo);

if (length > end - start)
{
start = i - (length - 1) / 2;
end = i + length / 2;
}
}
return input.Substring(start, end - start + 1);
}

private static int ExpandAroundCenter(string input, int left, int right)
{
while (left >= 0 && right < input.Length && input[left] == input[right])
{
left--;
right++;
}
return right - left - 1;
}

private static bool ContainsPalindrome(string input)
{
string[] words = input.Split(' ');
foreach (string word in words)
{
if (word.Length > 1 && word == Reverse(word))
{
return true;
}
}

string cleanedInput = input.Replace(" ", "");
return cleanedInput.Length > 1 && cleanedInput == Reverse(cleanedInput);
}

private static string Reverse(string input)
{
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
}

0 comments on commit 4ce60e7

Please sign in to comment.