Given two strings S1 and S2 of the same length containing characters ranging from ‘0’ to ‘9’, the task is to return the minimum number of operations required to make two strings equal by following the below operations:
- Increment any two characters from the string by 1.
- If it is not possible to make two strings equal with above condition then return -1.
Examples:
Input : S1 = “12”, S2 = “21”
Output : 1
Explanation: Operation 1: Increment first index of S1 and
second index of S2, new S1 = “22”, S2 = “22”
Input : “123”, S2 = “015”
Output : 2
Explanation: Operation 1: Increment third index of S1 and
first index of S2, new S1 = “124”, S2 = “115”
Operation 2: Increment third index of S1 and second index of S2.
New S1 = “125”, S2 = “125”
Input: S1 = “19”, S2 = “24”
Output: -1
Explanation: There is no such valid operation exists through which
we can make two strings equal.
Approach: To solve the problem follow the below observations:
Observations:
We are incrementing 1 to both the characters of strings to perform valid operation. So, if initially S1 and S2 have different sums, they can never be made equal (since they must have the same sum when they are equal). Now let, Sum(S1) = Sum(S2). Lets see for a specific index
0 ? i ? N – 1(N is length of either string S1 or S2).
- If S1[ i ] == s2[ i ], no operation will be needed
- If s1[ i ] > S2[ i ], then we need to add S1[ i ] – S2[ i ] in S2 to make S1[ i ] and S2[ i ] equal.
- If S1[ i ] < S2[ i ], then we need to add S2[ i ] – S1[ i ] in S1 to make S1[ i ] and S2[ i ] equal.
Let x is summation of S1[ i ] – S2[ i ] for all indices where S1[ i ] > S2[ i ]. As noted above, x is the minimum number of operations we need to perform on indices of S2.
Let y is summation of S2[ i ] – S1[ i ] for all indices where S1[ i ] < S2[ i ]. As noted above, y is the minimum number of operations we need to perform on indices of S1.
We will find the total summation of operations needed on both the strings i.e (x+y) and we will divide it by 2 because in every single operation we need to increase any character from both the strings.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimum_operations(string S1, string S2)
{
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < S1.size(); i++) {
sum1 += (S1[i] - '0' );
sum2 += (S2[i] - '0' );
}
if (sum1 != sum2)
return -1;
int count = 0;
for ( int i = 0; i < S1.size(); i++) {
if (S1[i] > S2[i]) {
count += (S1[i] - S2[i]);
}
else {
count += (S2[i] - S1[i]);
}
}
return count / 2;
}
int main()
{
string S1 = "123" ;
string S2 = "015" ;
cout << minimum_operations(S1, S2);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int minimum_operations(String S1,
String S2)
{
int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ; i < S1.length(); i++) {
sum1 += (S1.charAt(i) - '0' );
sum2 += (S2.charAt(i) - '0' );
}
if (sum1 != sum2)
return - 1 ;
int count = 0 ;
for ( int i = 0 ; i < S1.length(); i++) {
if (S1.charAt(i) > S2.charAt(i)) {
count += (S1.charAt(i) - S2.charAt(i));
}
else {
count += (S2.charAt(i) - S1.charAt(i));
}
}
return count / 2 ;
}
public static void main(String[] args)
{
String S1 = "123" ;
String S2 = "015" ;
System.out.print(minimum_operations(S1, S2));
}
}
|
Python3
def minimum_operations(S1, S2) :
sum1 = 0
sum2 = 0
for i in range ( len (S1)):
sum1 + = ( ord (S1[i]) - ord ( '0' ))
sum2 + = ( ord (S2[i]) - ord ( '0' ))
if (sum1 ! = sum2) :
return - 1
count = 0
for i in range ( len (S1)):
if (S1[i] > S2[i]) :
count + = ( ord (S1[i]) - ord (S2[i]))
else :
count + = ( ord (S2[i]) - ord (S1[i]))
return count / / 2
if __name__ = = "__main__" :
S1 = "123"
S2 = "015"
print (minimum_operations(S1, S2))
|
C#
using System;
public class GFG {
public static int minimum_operations(String S1,
String S2)
{
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < S1.Length; i++) {
sum1 += (S1[i] - '0' );
sum2 += (S2[i] - '0' );
}
if (sum1 != sum2)
return -1;
int count = 0;
for ( int i = 0; i < S1.Length; i++) {
if (S1[i] > S2[i]) {
count += (S1[i] - S2[i]);
}
else {
count += (S2[i] - S1[i]);
}
}
return count / 2;
}
static public void Main()
{
String S1 = "123" ;
String S2 = "015" ;
Console.Write(minimum_operations(S1, S2));
}
}
|
Javascript
<script>
const minimum_operations = (S1, S2) => {
let sum1 = 0, sum2 = 0;
for (let i = 0; i < S1.length; i++) {
sum1 += (S1.charCodeAt(i) - '0' .charCodeAt(0));
sum2 += (S2.charCodeAt(i) - '0' .charCodeAt(0));
}
if (sum1 != sum2)
return -1;
let count = 0;
for (let i = 0; i < S1.length; i++) {
if (S1[i] > S2[i]) {
count += (S1.charCodeAt(i) - S2.charCodeAt(i));
}
else {
count += (S2.charCodeAt(i) - S1.charCodeAt(i));
}
}
return parseInt(count / 2);
}
let S1 = "123" ;
let S2 = "015" ;
document.write(minimum_operations(S1, S2));
</script>
|
Time Complexity: O( N ), where N is the length of either string S1 or S2
Auxiliary Space: O( 1 )