Check if two strings are k | C # program for the above approach ; Function to check k anagram of two strings ; First Condition : If both the strings have different length , then they cannot form anagram ; Converting str1 to Character Array arr1 ; Converting str2 to Character Array arr2 ; Sort arr1 in increasing order ; Sort arr2 in increasing order ; Iterate till str1 . length ( ) ; Condition if arr1 [ i ] is not equal to arr2 [ i ] then add it to list ; Condition to check if strings for K - anagram or not ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; public class GFG { public static bool kAnagrams ( string str1 , string str2 , int k ) { int flag = 0 ; List < char > list = new List < char > ( ) ; if ( str1 . Length != str2 . Length ) { return false ; } char [ ] arr1 = str1 . ToCharArray ( ) ; char [ ] arr2 = str2 . ToCharArray ( ) ; Array . Sort ( arr1 ) ; Array . Sort ( arr2 ) ; for ( int i = 0 ; i < str1 . Length ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { list . Add ( arr2 [ i ] ) ; } } if ( list . Count <= k ) flag = 1 ; if ( flag == 1 ) return true ; else return false ; } static public void Main ( ) { string str1 = " fodr " ; string str2 = " gork " ; int k = 2 ; kAnagrams ( str1 , str2 , k ) ; if ( kAnagrams ( str1 , str2 , k ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }