Check if summation of two words is equal to target word | C ++ program for the above approach ; Function to check weather summation of two words equal to target word ; Store the length of each string ; Reverse the strings A , B and C ; Stores the remainder ; Iterate in the range [ 0 , max ( L , max ( M , N ) ) ] ; Stores the integer at ith position from the right in the sum of A and B ; If i is less than L ; If i is less than M ; Update rem and curr ; If i is less than N and curr is not equal to C [ i ] - ' a ' , return " No " ; If rem is greater than 0 , return " No " ; Otherwise , return " Yes " ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; }