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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to check whether summation // of two words equal to target word string isSumEqual(string A, string B, string C) { // Store the length of each string
int L = A.length();
int M = B.length();
int N = A.length();
// Reverse the strings A, B and C
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
reverse(C.begin(), C.end());
// Stores the remainder
int rem = 0;
// Iterate in the range
// [0, max(L, max(M, N))]
for ( int i = 0; i < max(L, max(M, N)); i++) {
// Stores the integer at ith
// position from the right in
// the sum of A and B
int curr = rem;
// If i is less than L
if (i < L)
curr += A[i] - 'a' ;
// If i is less than M
if (i < M)
curr += B[i] - 'a' ;
// Update rem and curr
rem = curr / 10;
curr %= 10;
// If i is less than N
// and curr is not equal
// to C[i]-'a', return "No"
if (i < N && curr != C[i] - 'a' ) {
return "No" ;
}
}
// If rem is greater
// than 0, return "No"
if (rem)
return "No" ;
// Otherwise, return "Yes"
else
return "Yes" ;
} // Driver Code int main()
{ // Given Input
string A = "acb" , B = "cba" , C = "cdb" ;
// Function Call
cout << isSumEqual(A, B, C);
return 0;
} |
// Java program for the above approach import java.util.*;
class GFG{
// Function to check whether summation // of two words equal to target word static String isSumEqual(String A, String B, String C)
{ // Store the length of each String
int L = A.length();
int M = B.length();
int N = A.length();
// Reverse the Strings A, B and C
A = reverse(A);
B = reverse(B);
C = reverse(C);
// Stores the remainder
int rem = 0 ;
// Iterate in the range
// [0, Math.max(L, Math.max(M, N))]
for ( int i = 0 ; i < Math.max(L, Math.max(M, N)); i++) {
// Stores the integer at ith
// position from the right in
// the sum of A and B
int curr = rem;
// If i is less than L
if (i < L)
curr += A.charAt(i) - 'a' ;
// If i is less than M
if (i < M)
curr += B.charAt(i) - 'a' ;
// Update rem and curr
rem = curr / 10 ;
curr %= 10 ;
// If i is less than N
// and curr is not equal
// to C[i]-'a', return "No"
if (i < N && curr != C.charAt(i) - 'a' ) {
return "No" ;
}
}
// If rem is greater
// than 0, return "No"
if (rem> 0 )
return "No" ;
// Otherwise, return "Yes"
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);
} // Driver Code public static void main(String[] args)
{ // Given Input
String A = "acb" , B = "cba" , C = "cdb" ;
// Function Call
System.out.print(isSumEqual(A, B, C));
} } // This code is contributed by 29AjayKumar |
# Python3 program for the above approach # Function to check whether summation # of two words equal to target word def isSumEqual(A, B, C):
# Store the length of each string
L = len (A)
M = len (B)
N = len (A)
# Reverse the strings A, B and C
A = A[:: - 1 ]
B = B[:: - 1 ]
C = C[:: - 1 ]
# Stores the remainder
rem = 0
# Iterate in the range
# [0, max(L, max(M, N))]
for i in range ( max (L, max (M, N))):
# Stores the integer at ith
# position from the right in
# the sum of A and B
curr = rem
# If i is less than L
if (i < L):
curr + = ord (A[i]) - ord ( 'a' )
# If i is less than M
if (i < M):
curr + = ord (B[i]) - ord ( 'a' )
# Update rem and curr
rem = curr / / 10
curr % = 10
# If i is less than N
# and curr is not equal
# to C[i]-'a', return "No"
if (i < N and curr ! = ord (C[i]) - ord ( 'a' )):
return "No"
# If rem is greater
# than 0, return "No"
if (rem):
return "No"
# Otherwise, return "Yes"
else :
return "Yes"
# Driver Code if __name__ = = '__main__' :
# Given Input
A = "acb"
B = "cba"
C = "cdb"
# Function Call
print (isSumEqual(A, B, C))
# This code is contributed by mohit kumar 29.
|
// C# program for the above approach using System;
public class GFG{
// Function to check whether summation // of two words equal to target word static String isSumEqual(String A, String B, String C)
{ // Store the length of each String
int L = A.Length;
int M = B.Length;
int N = A.Length;
// Reverse the Strings A, B and C
A = reverse(A);
B = reverse(B);
C = reverse(C);
// Stores the remainder
int rem = 0;
// Iterate in the range
// [0, Math.Max(L, Math.Max(M, N))]
for ( int i = 0; i < Math.Max(L, Math.Max(M, N)); i++) {
// Stores the integer at ith
// position from the right in
// the sum of A and B
int curr = rem;
// If i is less than L
if (i < L)
curr += A[i] - 'a' ;
// If i is less than M
if (i < M)
curr += B[i] - 'a' ;
// Update rem and curr
rem = curr / 10;
curr %= 10;
// If i is less than N
// and curr is not equal
// to C[i]-'a', return "No"
if (i < N && curr != C[i] - 'a' ) {
return "No" ;
}
}
// If rem is greater
// than 0, return "No"
if (rem>0)
return "No" ;
// Otherwise, return "Yes"
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);
} // Driver Code public static void Main(String[] args)
{ // Given Input
String A = "acb" , B = "cba" , C = "cdb" ;
// Function Call
Console.Write(isSumEqual(A, B, C));
} } // This code is contributed by 29AjayKumar |
<script> // JavaScript program for the above approach // Function to check whether summation // of two words equal to target word function isSumEqual(A, B, C) {
// Store the length of each string
let L = A.length;
let M = B.length;
let N = A.length;
// Reverse the strings A, B and C
A.split( "" ).reverse().join( "" );
B.split( "" ).reverse().join( "" );
C.split( "" ).reverse().join( "" );
// Stores the remainder
let rem = 0;
// Iterate in the range
// [0, max(L, max(M, N))]
for (let i = 0; i < Math.max(L, Math.max(M, N)); i++) {
// Stores the integer at ith
// position from the right in
// the sum of A and B
let curr = rem;
// If i is less than L
if (i < L)
curr += A[i].charCodeAt(0) - 'a' .charCodeAt(0);
// If i is less than M
if (i < M)
curr += B[i].charCodeAt(0) - 'a' .charCodeAt(0);
// Update rem and curr
rem = Math.floor(curr / 10);
curr %= 10;
// If i is less than N
// and curr is not equal
// to C[i]-'a', return "No"
if (i < N && curr != C[i].charCodeAt(0) -
'a' .charCodeAt(0)) {
return "No" ;
}
}
// If rem is greater
// than 0, return "No"
if (rem)
return "No" ;
// Otherwise, return "Yes"
else
return "Yes" ;
} // Driver Code // Given Input let A = "acb" , B = "cba" , C = "cdb" ;
// Function Call document.write(isSumEqual(A, B, C)); </script> |
Yes
Time Complexity: O(L+M+N)
Auxiliary Space: O(1)