Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1880. Check if Word Equals Summation of Two Words - Leetcode Solution

Code Implementation

class Solution:
    def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
        def word_to_number(word):
            return int(''.join(str(ord(c) - ord('a')) for c in word))
        return word_to_number(firstWord) + word_to_number(secondWord) == word_to_number(targetWord)
      
class Solution {
public:
    int wordToNumber(string word) {
        string numStr = "";
        for (char c : word) {
            numStr += to_string(c - 'a');
        }
        return stoi(numStr);
    }
    bool isSumEqual(string firstWord, string secondWord, string targetWord) {
        return wordToNumber(firstWord) + wordToNumber(secondWord) == wordToNumber(targetWord);
    }
};
      
class Solution {
    private int wordToNumber(String word) {
        StringBuilder sb = new StringBuilder();
        for (char c : word.toCharArray()) {
            sb.append((int)(c - 'a'));
        }
        return Integer.parseInt(sb.toString());
    }
    public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
        return wordToNumber(firstWord) + wordToNumber(secondWord) == wordToNumber(targetWord);
    }
}
      
var isSumEqual = function(firstWord, secondWord, targetWord) {
    function wordToNumber(word) {
        return parseInt([...word].map(c => c.charCodeAt(0) - 'a'.charCodeAt(0)).join(''));
    }
    return wordToNumber(firstWord) + wordToNumber(secondWord) === wordToNumber(targetWord);
};
      

Problem Description

Given three strings firstWord, secondWord, and targetWord consisting of lowercase English letters, you are asked to check whether the sum of the "numerical values" of firstWord and secondWord equals the numerical value of targetWord.

The "numerical value" of a word is defined by replacing each letter with its position in the alphabet starting from 0 ('a' = 0, 'b' = 1, ..., 'j' = 9), then concatenating the numbers to form an integer. For example, "acb" becomes "021" which is 21 as an integer.

Your task is to determine if:
numericalValue(firstWord) + numericalValue(secondWord) == numericalValue(targetWord)

  • Each input string contains only lowercase English letters.
  • There is no reuse of elements or ambiguity in conversion; each letter is mapped directly to its corresponding digit.

Thought Process

To solve this problem, we first need to understand how to convert a word into its numerical value as described. Each letter is mapped to a digit (with 'a' as 0, 'b' as 1, and so on), and the digits are concatenated to form a number. For instance, "abc" becomes 012, which is 12 as an integer.

The brute-force approach would involve converting each word manually, possibly using a loop for each character. However, since the mapping is straightforward and the word lengths are small, this is already efficient. There is no need for complex optimizations such as hash maps or memoization.

The key insight is that this is a simple transformation and comparison problem. Once we have a helper function or logic to convert a word to its number, we just check if the sum of the first two equals the third.

Solution Approach

  • Step 1: Define a function to convert a word to its numerical value.
    • For each character in the word, subtract the ASCII value of 'a' from the character to get its digit value (e.g., ord('c') - ord('a') = 2).
    • Concatenate these digits as strings.
    • Convert the concatenated string to an integer.
  • Step 2: Use this function for all three words.
    • Compute the numerical value for firstWord, secondWord, and targetWord.
  • Step 3: Compare the sum of the first two to the third.
    • If the sum equals the target, return true; otherwise, return false.

This approach is direct and efficient because string manipulation and integer conversion are fast, and the input sizes are small.

Example Walkthrough

Suppose we have:

  • firstWord = "acb"
  • secondWord = "cba"
  • targetWord = "cdb"

  1. Convert "acb" to a number:
    • 'a' → 0, 'c' → 2, 'b' → 1
    • Concatenate: "021" → 21
  2. Convert "cba" to a number:
    • 'c' → 2, 'b' → 1, 'a' → 0
    • Concatenate: "210" → 210
  3. Convert "cdb" to a number:
    • 'c' → 2, 'd' → 3, 'b' → 1
    • Concatenate: "231" → 231
  4. Sum and compare:
    • 21 + 210 = 231
    • 231 == 231 → True

The function would return true for this example.

Time and Space Complexity

Brute-force approach: Even the brute-force approach is linear in the length of the words, because we process each character once.

  • Time Complexity: O(N), where N is the total number of characters in all three words. Each character is processed exactly once.
  • Space Complexity: O(N) for storing the intermediate strings during conversion. If you optimize by building the number directly, it's O(1) extra space.

Optimized Approach: The solution described is already optimal for this problem, given the simplicity of the transformation.

Summary

This problem boils down to a simple string-to-number transformation using a fixed mapping from letters to digits. The key insight is recognizing that each letter's value can be computed directly using ASCII arithmetic, and the concatenation forms the number. The approach is both intuitive and efficient, making it a great example of how understanding the problem's structure can lead to an elegant and straightforward solution.