Check if Word Equals Summation of Two Words

Below is the Python code to determine whether the summation of the numerical values of firstWord and secondWord equals the numerical value of targetWord, following the given constraints and using lower case English letters ‘a’ through ‘j’.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution:
    def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
        # Function to convert word into numerical value
        def get_value(word):
            value = ""
            for letter in word:
                # Convert each letter into its numerical value and concatenate
                value += str(ord(letter) - ord('a'))
            return int(value)

        # Calculate numerical values of the first, second and target words
        first_value = get_value(firstWord)
        second_value = get_value(secondWord)
        target_value = get_value(targetWord)

        # Check if summation of first and second values equals target value
        return first_value + second_value == target_value

Explanation:

  1. Define a Function to Convert Word: We define a function get_value that takes a word and converts it into a numerical value according to the rules specified in the problem. This involves converting each letter to its corresponding value and concatenating them together.
  2. Convert Words to Numerical Values: We call the get_value function on firstWord, secondWord, and targetWord to convert them into their corresponding numerical values.
  3. Check Summation: We check if the summation of the numerical values of firstWord and secondWord equals the numerical value of targetWord. If it does, we return True; otherwise, we return False.