// Returns longest Palindrome string method 2 public static string GetLongestPalindromesSubString(string source) { List <string> subsets = new List <string>(); for (int i = 0; i < source.Length - 1; i++) { for (int j = i + 1; j <= source.Length; j++) { if (j - i > 1 && source[j - 1] == source[i]) //if (source[i] == source[j]) { string currentSubset = source.Substring(i, j - i); //string currentSubset = source.Substring(i,j-i); if (CheckStringPalindrome.CheckWhetherStringIsPalindrome(currentSubset, true)) { subsets.Add(currentSubset); } } } } int indexMax = !subsets.Any() ? -1 : subsets .Select((value, index) => new { Value = value, Index = index }) .Aggregate((a, b) => (a.Value.Length > b.Value.Length) ? a : b) .Index; return(subsets[indexMax]); }
// Function to find all unique palindromic substrings of given String public static List <string> GetDistinctPalindromes(string source) { List <string> subsets = new List <string>(); for (int i = 0; i < source.Length - 1; i++) { for (int j = i + 1; j <= source.Length; j++) { if (j - i > 1 && source[j - 1] == source[i]) //if (source[i] == source[j]) { string currentSubset = source.Substring(i, j - i); //string currentSubset = source.Substring(i,j-i); if (CheckStringPalindrome.CheckWhetherStringIsPalindrome(currentSubset, true)) { subsets.Add(currentSubset); } } } } return(subsets); }