Check if summation of two words is equal to target word | Java 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 , Math . max ( L , Math . 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
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 ) ) ; } }