// C# program to find if given
// String is K-Palindrome or not
using System;
class GFG
{
/* Returns length of LCS for
X[0..m-1], Y[0..n-1] */
static int lcs(String X, String Y,
int m, int n)
{
int [,]L = new int[m + 1,n + 1];
/* Following steps build L[m+1,n+1]
in bottom up fashion. Note that L[i,j]
contains length of LCS of X[0..i-1]
and Y[0..j-1] */
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
L[i, j] = 0;
}
else if (X[i - 1] == Y[j - 1])
{
L[i, j] = L[i - 1, j - 1] + 1;
}
else
{
L[i, j] = Math.Max(L[i - 1, j],
L[i, j - 1]);
}
}
}
// L[m,n] contains length
// of LCS for X and Y
return L[m, n];
}
// find if given String is
// K-Palindrome or not
static bool isKPal(String str, int k)
{
int n = str.Length;
// Find reverse of String
str = reverse(str);
// find longest palindromic
// subsequence of given String
int lps = lcs(str, str, n, n);
// If the difference between longest
// palindromic subsequence and the
// original String is less than equal
// to k, then the String is k-palindrome
return (n - lps <= k);
}
static String reverse(String input)
{
char[] temparray = input.ToCharArray();
int left, right = 0;
right = temparray.Length - 1;
for (left = 0; left < right; left++, right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join("",temparray);
}
// Driver code
public static void Main(String[] args)
{
String str = "abcdeca";
int k = 2;
if (isKPal(str, k))
{
Console.WriteLine("Yes");
}
else
Console.WriteLine("No");
}
}
// This code is contributed by PrinciRaj1992