Given two N-length strings S and T consisting of lowercase alphabets, the task is to minimize the number of swaps of same indexed elements required to make the sum of ASCII value of characters of both the strings odd. If it is not possible to make the sum of ASCII values odd, then print “-1”.
Examples:
Input:S = ”acd”, T = ”dbf”
Output: 1
Explanation:
Swapping S[1] and T[1] modifies S to “abd” and T to “dcf”.
Sum of ASCII value of characters of the string S = 97 + 98 + 100 = 297 (Odd).
Sum of ASCII value of characters of the string T = 100 + 99 + 102 = 301 (Odd).
Input: S = “aey”, T = “cgj”
Output: -1
Approach: Follow the steps below to solve the problem:
- Calculate the sum of ASCII values of the characters of the string S and T and store it in variables sum1 and sum2 respectively.
- If sum1 and sum2 are already odd, then print 0, as no swaps are required.
- If sum1 and sum2 are of different parities, print -1, as the sum cannot be of same parity for both the strings.
- If sum1 and sum2 are both even, then traverse the given strings S and T. If there exists any character with odd ASCII value, sum of ASCII values of the characters of both the strings can be made odd by only 1 swap. Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countSwaps(string S, string T)
{
int value[26];
for ( int i = 0; i < 26; i++)
value[i] = i + 1;
int N = S.size();
int sum1 = 0;
int sum2 = 0;
bool flag = false ;
for ( int i = 0; i < N; i++) {
sum1 += value[S[i] - 'a' ];
sum2 += value[T[i] - 'a' ];
if ((value[S[i] - 'a' ] % 2 == 0
&& value[T[i] - 'a' ] % 2 == 1)
|| (value[S[i] - 'a' ] % 2 == 1
&& value[T[i] - 'a' ] % 2 == 0))
flag = false ;
}
if (sum1 % 2 == 1
&& sum2 % 2 == 1)
cout << "0\n" ;
else if (sum1 % 2 == 0
&& sum2 % 2 == 0) {
if (flag)
cout << "1" ;
else
cout << "-1" ;
}
else {
cout << "-1" ;
}
}
int main()
{
string S = "acd" ;
string T = "dbf" ;
countSwaps(S, T);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void countSwaps(String S, String T)
{
int [] value = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++)
value[i] = i + 1 ;
int N = S.length();
int sum1 = 0 ;
int sum2 = 0 ;
boolean flag = false ;
for ( int i = 0 ; i < N; i++) {
sum1 += value[S.charAt(i) - 'a' ];
sum2 += value[T.charAt(i) - 'a' ];
if ((value[S.charAt(i) - 'a' ] % 2 == 0
&& value[T.charAt(i) - 'a' ] % 2 == 1 )
|| (value[S.charAt(i) - 'a' ] % 2 == 1
&& value[T.charAt(i) - 'a' ] % 2 == 0 ))
flag = false ;
}
if (sum1 % 2 == 1
&& sum2 % 2 == 1 )
System.out.println( "0\n" );
else if (sum1 % 2 == 0
&& sum2 % 2 == 0 ) {
if (flag)
System.out.println( "1" );
else
System.out.println( "-1" );
}
else {
System.out.println( "-1" );
}
}
public static void main(String[] args)
{
String S = "acd" ;
String T = "dbf" ;
countSwaps(S, T);
}
}
|
Python3
def countSwaps(S, T):
value = [ 0 ] * 26
for i in range ( 26 ):
value[i] = i + 1
N = len (S)
sum1 = 0
sum2 = 0
flag = False
for i in range (N):
sum1 + = value[ ord (S[i]) - ord ( 'a' )]
sum2 + = value[ ord (T[i]) - ord ( 'a' )]
if (value[ ord (S[i]) - ord ( 'a' )] % 2 = = 0
and value[ ord (T[i]) - ord ( 'a' )] % 2 = = 1
or value[ ord (S[i]) - ord ( 'a' )] % 2 = = 1
and value[ ord (T[i]) - ord ( 'a' )] % 2 = = 0 ):
flag = False
if (sum1 % 2 = = 1 and sum2 % 2 = = 1 ):
print ( "0" )
elif (sum1 % 2 = = 0 and sum2 % 2 = = 0 ):
if (flag):
print ( "1" )
else :
print ( "-1" )
else :
print ( "-1" )
if __name__ = = '__main__' :
S = "acd"
T = "dbf"
countSwaps(S, T)
|
C#
using System;
public class GFG
{
static void countSwaps( string S, string T)
{
int [] value = new int [26];
for ( int i = 0; i < 26; i++)
value[i] = i + 1;
int N = S.Length;
int sum1 = 0;
int sum2 = 0;
bool flag = false ;
for ( int i = 0; i < N; i++) {
sum1 += value[S[i] - 'a' ];
sum2 += value[T[i] - 'a' ];
if ((value[S[i] - 'a' ] % 2 == 0
&& value[T[i] - 'a' ] % 2 == 1)
|| (value[S[i] - 'a' ] % 2 == 1
&& value[T[i] - 'a' ] % 2 == 0))
flag = false ;
}
if (sum1 % 2 == 1
&& sum2 % 2 == 1)
Console.Write( "0\n" );
else if (sum1 % 2 == 0
&& sum2 % 2 == 0) {
if (flag)
Console.Write( "1" );
else
Console.Write( "-1" );
}
else {
Console.Write( "-1" );
}
}
public static void Main(String[] args)
{
string S = "acd" ;
string T = "dbf" ;
countSwaps(S, T);
}
}
|
Javascript
<script>
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] - "a" ];
sum2 += value[T[i] - "a" ];
if (
(value[S[i] - "a" ] % 2 === 0 && value[T[i] - "a" ] % 2 === 1) ||
(value[S[i] - "a" ] % 2 === 1 && value[T[i] - "a" ] % 2 === 0)
)
flag = false ;
}
if (sum1 % 2 === 1 && sum2 % 2 === 1) document.write( "0 <br>" );
else if (sum1 % 2 === 0 && sum2 % 2 === 0) {
if (flag) document.write( "1" );
else document.write( "-1" );
}
else {
document.write( "-1" );
}
}
var S = "aey" ;
var T = "cgj" ;
countSwaps(S, T);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(26), as we are using extra space for value array.