Minimize swaps of same | Function to count the number of swaps required to make the sum of ASCII values of the characters of both strings odd ; Initialize alphabets with value ; Initialize values for each alphabet ; Size of the string ; Sum of string S ; Sum of string T ; Stores whether there is any index i such that S [ i ] and T [ i ] have different parities ; Traverse the strings ; Update sum1 and sum2 ; If S [ i ] and T [ i ] have different parities ; If sum1 and sum2 are both odd ; If sum1 and sum2 are both even ; If exists print 1 ; Otherwise ; If sum1 and sum2 are of different parities ; Driver Code ; Function Call
function countSwaps ( S , T ) { var value = [ ... Array ( 26 ) ] ; for ( var i = 0 ; i < 26 ; i ++ ) value [ i ] = i + 1 ; var N = S . length ; var sum1 = 0 ; var sum2 = 0 ; var flag = false ; for ( var i = 0 ; i < N ; i ++ ) { sum1 += value [ S [ i ] - " " ] ; sum2 += value [ T [ i ] - " " ] ; if ( ( value [ S [ i ] - " " ] % 2 === 0 && value [ T [ i ] - " " ] % 2 === 1 ) || ( value [ S [ i ] - " " ] % 2 === 1 && value [ T [ i ] - " " ] % 2 === 0 ) ) flag = false ; } if ( sum1 % 2 === 1 && sum2 % 2 === 1 ) document . write ( " " ) ; else if ( sum1 % 2 === 0 && sum2 % 2 === 0 ) { if ( flag ) document . write ( " " ) ; else document . write ( " " ) ; } else { document . write ( " " ) ; } } var S = " " ; var T = " " ; countSwaps ( S , T ) ;