{x}
blog image

Check if Word Equals Summation of Two Words

The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.).

The numerical value of some string of lowercase English letters s is the concatenation of the letter values of each letter in s, which is then converted into an integer.

  • For example, if s = "acb", we concatenate each letter's letter value, resulting in "021". After converting it, we get 21.

You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase English letters 'a' through 'j' inclusive.

Return true if the summation of the numerical values of firstWord and secondWord equals the numerical value of targetWord, or false otherwise.

 

Example 1:

Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb"
Output: true
Explanation:
The numerical value of firstWord is "acb" -> "021" -> 21.
The numerical value of secondWord is "cba" -> "210" -> 210.
The numerical value of targetWord is "cdb" -> "231" -> 231.
We return true because 21 + 210 == 231.

Example 2:

Input: firstWord = "aaa", secondWord = "a", targetWord = "aab"
Output: false
Explanation: 
The numerical value of firstWord is "aaa" -> "000" -> 0.
The numerical value of secondWord is "a" -> "0" -> 0.
The numerical value of targetWord is "aab" -> "001" -> 1.
We return false because 0 + 0 != 1.

Example 3:

Input: firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
Output: true
Explanation: 
The numerical value of firstWord is "aaa" -> "000" -> 0.
The numerical value of secondWord is "a" -> "0" -> 0.
The numerical value of targetWord is "aaaa" -> "0000" -> 0.
We return true because 0 + 0 == 0.

 

Constraints:

  • 1 <= firstWord.length, secondWord.length, targetWord.length <= 8
  • firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive.

Solution Explanation

This problem involves converting words into numerical values based on the alphabetical position of their constituent letters and then checking if the sum of the numerical values of two words equals the numerical value of a third word.

Core Logic:

  1. Numerical Value Calculation: The core of the solution is a function (f in the code examples) that takes a word as input and calculates its numerical value. It iterates through each character in the word:

    • It gets the ASCII value of the character.
    • It subtracts the ASCII value of 'a' to get the letter's position (0 for 'a', 1 for 'b', etc.).
    • It builds the numerical value by multiplying the current value by 10 and adding the new digit. This effectively concatenates the letter values as digits.
  2. Summation and Comparison: Once the f function is defined, the main part of the solution simply calculates the numerical values of firstWord and secondWord using f, sums them, and compares the sum to the numerical value of targetWord (also calculated using f). It returns true if they are equal and false otherwise.

Code Examples (Python, Java, C++, Go, TypeScript, Rust, Javascript, C):

All the provided code examples follow the same logic: they define the f function for numerical value calculation and then use it to perform the comparison. The only differences are in the syntax and specifics of each language. For instance:

  • Python: Uses ord() to get the ASCII value and a concise map function.
  • Java: Employs a for-each loop for character iteration.
  • C++: Utilizes a lambda expression for the f function.
  • Go: Uses a for...range loop for iteration.
  • TypeScript: Similar to JavaScript, uses charCodeAt(0) to access ASCII value.
  • Rust: Uses character iteration and pattern matching for a concise solution.
  • JavaScript: Uses a for...of loop and charCodeAt(0) similarly to TypeScript.
  • C: Uses a while loop and pointer arithmetic for iteration.

Time Complexity Analysis:

The time complexity is O(N), where N is the total number of characters across all three input words. This is because the f function iterates through each character of the input strings once to calculate the numerical values. The summation and comparison operations take constant time.

Space Complexity Analysis:

The space complexity is O(1). The algorithm uses a fixed amount of extra space regardless of the input size. The space used is primarily for storing the numerical values and intermediate calculations, which are independent of the length of the input words.

Example Walkthrough (Python):

Let's trace the example firstWord = "acb", secondWord = "cba", targetWord = "cdb":

  1. f("acb"): 'a' -> 0, 'c' -> 2, 'b' -> 1. Concatenated: "021". Integer value: 21.
  2. f("cba"): 'c' -> 2, 'b' -> 1, 'a' -> 0. Concatenated: "210". Integer value: 210.
  3. f("cdb"): 'c' -> 2, 'd' -> 3, 'b' -> 1. Concatenated: "231". Integer value: 231.
  4. 21 + 210 == 231, so the function returns True.