Check if two strings can be made equal by swapping one character among each other | C # implementation of the above approach ; Function that returns true if the string can be made equal after one swap ; A and B are new a and b after we omit the same elements ; Take only the characters which are different in both the strings for every pair of indices ; If the current characters differ ; The strings were already equal ; If the lengths of the strings are two ; If swapping these characters can make the strings equal ; Driver code
using System ; using System . Collections . Generic ; class GFG { static Boolean canBeEqual ( char [ ] a , char [ ] b , int n ) { List < char > A = new List < char > ( ) ; List < char > B = new List < char > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { A . Add ( a [ i ] ) ; B . Add ( b [ i ] ) ; } } if ( A . Count == B . Count && B . Count == 0 ) return true ; if ( A . Count == B . Count && B . Count == 2 ) { if ( A [ 0 ] == A [ 1 ] && B [ 0 ] == B [ 1 ] ) return true ; } return false ; } public static void Main ( String [ ] args ) { char [ ] A = " SEEKSFORGEEKS " . ToCharArray ( ) ; char [ ] B = " GEEKSFORGEEKG " . ToCharArray ( ) ; if ( canBeEqual ( A , B , A . Length ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }