Check if the summation of two words is equal to the target word


We will be given three strings str1, str2, and str3 in this problem of the same or different lengths and we have to find whether the sum of the first two strings is equal to the third or not. Each of the strings contains elements less than ‘k’ which means ‘a’ can be decoded as ‘0’ and j as ‘9’ and we can take the sum of them as the normal numbers.

Sample Examples

Input 1

string str1 = “abc”

string str2 = “bbe”

string str3 = “cdg”

Output: Yes

Explanation − We can decode ‘a’ as ‘0’, ‘b’ as ‘1’, c as ‘2’, d as ‘3’, e as ‘4’, and g as ‘6’. After mapping the decoded values, we will get the final strings as “012”, “114”, and “236”. We can add the first two to get the third value.

Input 2

string str1 = “abc”

string str2 = “abca”

string str3 = “aceb”

Output: No

Explanation − We can decode ‘a’ as ‘0’, ‘b’ as ‘1’, and c as ‘2’. After mapping the decoded values, we will get the final strings as “012”, “0120”, and “0241”. We cannot add the first two to get the third value.

Approach

We have seen the examples above, now let us move to the main approach steps that we are going to implement to get the required result.

  • First, we will create a function that will return a boolean value indicating the result and it will take the parameter of all the three given strings.

  • In the function first, we will get the length of all three strings that will help to traverse over them by using the while or for loop.

  • We will reverse all the given strings because we are going to add the string and as we add the numbers from the last, so to add the string from last we will reverse them to make the process easy.

  • We will use the while loop to traverse over the strings and will create a variable carry to store the mathematical carry used for addition.

  • For the current index, we will add both string current index ASCII values and will subtract the ASCII value of ‘a’ to make the sum easy and store them in the carry.

  • For the carry variable, we will add the mod 10 value plus ‘a’ in the answer string and divide the carry by 10.

  • After traversing over the strings, we will compare the result with the third given string and as the strings are already reversed so we can compare directly.

  • From the main function, we will call the function and will print the result on the basis of the return value.

Example

#include <bits/stdc++.h>
using namespace std;
bool isEqual(string str1, string str2, string str3){
   // getting lengths of the string 
   int len1 = str1.size();
   int len2 = str2.size();
   int len3 = str3.size();   
   // reversing the strings 
   reverse(str1.begin(),str1.end());
   reverse(str2.begin(),str2.end());
   reverse(str3.begin(),str3.end());    
   // Getting the sum of the first two strings 
   string ans = "";
   int carry = 0; // variable to store the carry     
   int i=0, j=0;
   while(i < len1 || j < len2){
      if(i < len1 && j < len2){
         carry += str1[i]-'a'+str2[j]-'a';
         ans += 'a'+carry%10;
         carry /= 10;
         i++;
         j++;
      }
      else if(i == len1){
         carry += str2[j]-'a';
         ans += 'a'+carry%10;
         carry /= 10;
         j++;
      }
      else{
         carry += str1[i]-'a';
         ans += 'a'+carry%10;
         carry /= 10;
         i++;
      }
   }
   if(carry){
      ans += 'b';
   }
   return ans == str3; // returning the value on the basis of condition
}
int main(){
   // defining the given strings
   string str1 = "abc";
   string str2 = "bbe";
   string str3 = "bcg";    
   // calling the function to get if the given condition is true or not 
   if(isEqual(str1, str2, str3)){
      cout<<"Yes, the given strings have the sum equal to the given third string"<<endl;
   }
   else{
      cout<<"No, the given strings do not have the sum equal to the given third string"<<endl;
   }
   return 0;
}

Output

Yes, the given strings have the sum equal to the given third string

Time and Space Complexity

The time complexity of the above code is O(N) where N is the length of the given string. As we are moving over the given string only once for both and once, we are reversing all of them.

The space complexity of the above code is O(N), because we are storing the result in the extra string.

Conclusion

In the above tutorial, we have implemented a program to find whether the given two strings have a sum equal to the given third string. We have used the concept of the ASCII value and basic mathematics to add the strings. The time complexity of the above code is O(N) and the same space complexity.

Updated on: 17-May-2023

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements