Find if string is K | C # program to find if given String is K - Palindrome or not ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . 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 ] ; L [ m , n ] contains length of LCS for X and Y ; find if given String is K - Palindrome or not ; Find reverse of String ; find longest palindromic subsequence of given String ; If the difference between longest palindromic subsequence and the original String is less than equal to k , then the String is k - palindrome ; Swap values of left and right ; Driver code
using System ; class GFG { static int lcs ( String X , String Y , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 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 ] ) ; } } } return L [ m , n ] ; } static bool isKPal ( String str , int k ) { int n = str . Length ; str = reverse ( str ) ; int lps = lcs ( str , str , n , n ) ; 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 -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { String str = " abcdeca " ; int k = 2 ; if ( isKPal ( str , k ) ) { Console . WriteLine ( " Yes " ) ; } else Console . WriteLine ( " No " ) ; } }