Check if summation of two words is equal to target word
Given three strings A, B, and C of size L, M, and N respectively and consisting of only lower case English alphabets less than ‘K’. The task is to check if the sum of strings A and B is equal to the string C after decoding the strings into integers by mapping alphabets with their index value in the list of alphabets and concatenating them.
Examples:
Input: A = “acb”, B = “cba”, C = “cdb”
Output: Yes
Explanation:
- The string A, modifies to integer 021 after replacing the characters ‘a’, ‘b’ and ‘c’ with their index values in the list of alphabets i.e 0, 1 and 2.
- The string B, modifies to integer 210 after replacing the characters ‘a’, ‘b’ and ‘c’ with their index values in the list of alphabets i.e 0, 1 and 2.
- The string C, modifies to integer 231 after replacing the characters ‘b’, ‘c’ and ‘d’ with their index values in the list of alphabets i.e 1, 2 and 3.
The sum of strings A and B i.e (21+210 = 231) is equal to 231, which is the value of string C. Therefore, print “Yes”.
Input: A = “aaa”, B = “bcb”, C = “bca”
Output: No
Approach: The problem can be solved using a similar approach used in finding the sum of two large numbers represented as strings. Follow the steps below to solve the problem:
- Reverse the strings A, B, and C.
- Initialize two variables, say curr and rem as 0 to store the value at ith position and the remainder of the sum of the strings A and B.
- Iterate over the range [0, max(L, max(M, N))] using the variable i and performing the following steps:
- Store the sum of characters at the ith index of the strings A and B in the variable curr.
- Update curr as curr = curr+rem and then update rem as rem = curr/10.
- Now check if i is less than N and curr%10 is not equal to the C[i]-‘a’ i.e value at the ith character of the string C, then print “No” and return.
- Finally, after completing the above steps, if rem is greater than 0 then print “No“. Otherwise, print “Yes“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string isSumEqual(string A, string B, string C)
{
int L = A.length();
int M = B.length();
int N = A.length();
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
reverse(C.begin(), C.end());
int rem = 0;
for ( int i = 0; i < max(L, max(M, N)); i++) {
int curr = rem;
if (i < L)
curr += A[i] - 'a' ;
if (i < M)
curr += B[i] - 'a' ;
rem = curr / 10;
curr %= 10;
if (i < N && curr != C[i] - 'a' ) {
return "No" ;
}
}
if (rem)
return "No" ;
else
return "Yes" ;
}
int main()
{
string A = "acb" , B = "cba" , C = "cdb" ;
cout << isSumEqual(A, B, C);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String isSumEqual(String A, String B, String C)
{
int L = A.length();
int M = B.length();
int N = A.length();
A = reverse(A);
B = reverse(B);
C = reverse(C);
int rem = 0 ;
for ( int i = 0 ; i < Math.max(L, Math.max(M, N)); i++) {
int curr = rem;
if (i < L)
curr += A.charAt(i) - 'a' ;
if (i < M)
curr += B.charAt(i) - 'a' ;
rem = curr / 10 ;
curr %= 10 ;
if (i < N && curr != C.charAt(i) - 'a' ) {
return "No" ;
}
}
if (rem> 0 )
return "No" ;
else
return "Yes" ;
}
static String reverse(String input) {
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String A = "acb" , B = "cba" , C = "cdb" ;
System.out.print(isSumEqual(A, B, C));
}
}
|
Python3
def isSumEqual(A, B, C):
L = len (A)
M = len (B)
N = len (A)
A = A[:: - 1 ]
B = B[:: - 1 ]
C = C[:: - 1 ]
rem = 0
for i in range ( max (L, max (M, N))):
curr = rem
if (i < L):
curr + = ord (A[i]) - ord ( 'a' )
if (i < M):
curr + = ord (B[i]) - ord ( 'a' )
rem = curr / / 10
curr % = 10
if (i < N and curr ! = ord (C[i]) - ord ( 'a' )):
return "No"
if (rem):
return "No"
else :
return "Yes"
if __name__ = = '__main__' :
A = "acb"
B = "cba"
C = "cdb"
print (isSumEqual(A, B, C))
|
C#
using System;
public class GFG{
static String isSumEqual(String A, String B, String C)
{
int L = A.Length;
int M = B.Length;
int N = A.Length;
A = reverse(A);
B = reverse(B);
C = reverse(C);
int rem = 0;
for ( int i = 0; i < Math.Max(L, Math.Max(M, N)); i++) {
int curr = rem;
if (i < L)
curr += A[i] - 'a' ;
if (i < M)
curr += B[i] - 'a' ;
rem = curr / 10;
curr %= 10;
if (i < N && curr != C[i] - 'a' ) {
return "No" ;
}
}
if (rem>0)
return "No" ;
else
return "Yes" ;
}
static String reverse(String input) {
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
public static void Main(String[] args)
{
String A = "acb" , B = "cba" , C = "cdb" ;
Console.Write(isSumEqual(A, B, C));
}
}
|
Javascript
<script>
function isSumEqual(A, B, C) {
let L = A.length;
let M = B.length;
let N = A.length;
A.split( "" ).reverse().join( "" );
B.split( "" ).reverse().join( "" );
C.split( "" ).reverse().join( "" );
let rem = 0;
for (let i = 0; i < Math.max(L, Math.max(M, N)); i++) {
let curr = rem;
if (i < L)
curr += A[i].charCodeAt(0) - 'a' .charCodeAt(0);
if (i < M)
curr += B[i].charCodeAt(0) - 'a' .charCodeAt(0);
rem = Math.floor(curr / 10);
curr %= 10;
if (i < N && curr != C[i].charCodeAt(0) -
'a' .charCodeAt(0)) {
return "No" ;
}
}
if (rem)
return "No" ;
else
return "Yes" ;
}
let A = "acb" , B = "cba" , C = "cdb" ;
document.write(isSumEqual(A, B, C));
</script>
|
Time Complexity: O(L+M+N)
Auxiliary Space: O(1)
Last Updated :
12 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...