text: Check if a string can be made equal to another string by swapping or replacement of characters | C # program for the above approach ; Function to find if given Strings are same or not ; Base Condition ; Stores frequency of characters of the String str1 and str2 ; Traverse Strings str1 & str2 and store frequencies in a [ ] and b [ ] ; Check if both Strings have same characters or not ; If a character is present in one String and is not in another String , return false ; Sort the array a [ ] and b [ ] ; Check arrays a and b contain the same frequency or not ; If the frequencies are not the same after sorting ; At this point , str1 can be converted to str2 ; Driver Code
code: using System ; class GFG { static bool sameStrings ( string str1 , string str2 ) { int N = str1 . Length ; int M = str2 . Length ; if ( N != M ) { return false ; } int [ ] a = new int [ 256 ] ; int [ ] b = new int [ 256 ] ; for ( int j = 0 ; j < N ; j ++ ) { a [ str1 [ j ] - ' a ' ] ++ ; b [ str2 [ j ] - ' a ' ] ++ ; } int i = 0 ; while ( i < 256 ) { if ( ( a [ i ] == 0 && b [ i ] == 0 ) || ( a [ i ] != 0 && b [ i ] != 0 ) ) { i ++ ; } else { return false ; } } Array . Sort ( a ) ; Array . Sort ( b ) ; for ( int j = 0 ; j < 256 ; j ++ ) { if ( a [ j ] != b [ j ] ) return false ; } return true ; } static public void Main ( ) { string S1 = " cabbba " , S2 = " abbccc " ; if ( sameStrings ( S1 , S2 ) ) Console . Write ( " YES " + " STRNEWLINE " ) ; else Console . Write ( " ▁ NO " + " STRNEWLINE " ) ; } }