title: Check if Word Equals Summation of Two Words
slug: check-if-word-equals-summation-of-two-words
difficulty: easy
leetcode_id: 1880
leetcode_url: https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/
categories:
  - strings
patterns:
  - slug: greedy
    is_optimal: true

function_signature: "def is_sum_equal(first_word: str, second_word: str, target_word: str) -> bool:"

test_cases:
  visible:
    - input: { first_word: "acb", second_word: "cba", target_word: "cdb" }
      expected: true
    - input: { first_word: "aaa", second_word: "a", target_word: "aab" }
      expected: false
    - input: { first_word: "aaa", second_word: "a", target_word: "aaaa" }
      expected: true
  hidden:
    - input: { first_word: "a", second_word: "a", target_word: "aa" }
      expected: false
    - input: { first_word: "j", second_word: "j", target_word: "bi" }
      expected: true
    - input: { first_word: "ab", second_word: "cd", target_word: "ef" }
      expected: false
    - input: { first_word: "a", second_word: "b", target_word: "b" }
      expected: true

description: |
  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*.

constraints: |
  - `1 <= firstWord.length, secondWord.length, targetWord.length <= 8`
  - `firstWord`, `secondWord`, and `targetWord` consist of lowercase English letters from `'a'` to `'j'` **inclusive**

examples:
  - 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.'
  - 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.'
  - 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.'

explanation:
  intuition: |
    Think of this problem like a secret code where each letter maps to a digit. The letters `'a'` through `'j'` correspond to digits `0` through `9`. When you string these digits together, you form a number — just like how the digits `2`, `1`, `0` form the number `210`.

    The key insight is that we're not adding up individual letter values; we're treating them as **positional digits** in a decimal number. The letter `'c'` in `"cba"` isn't contributing `2` to a sum — it's contributing `200` because it's in the hundreds place.

    Once you recognise this, the problem becomes straightforward: convert each word to its numerical value, then check if the sum of the first two equals the third.

    The constraint that letters are limited to `'a'` through `'j'` ensures we only deal with single digits (`0-9`), making the conversion clean and unambiguous.

  approach: |
    We solve this by converting each word to its numerical value and comparing:

    **Step 1: Create a helper function to convert a word to its numerical value**

    - Iterate through each character in the word
    - For each character, calculate its digit value: `ord(char) - ord('a')` gives `0` for `'a'`, `1` for `'b'`, etc.
    - Build the number by treating digits positionally: `result = result * 10 + digit`

    &nbsp;

    **Step 2: Convert all three words**

    - Apply the conversion function to `firstWord`, `secondWord`, and `targetWord`
    - This gives us three integers to compare

    &nbsp;

    **Step 3: Return the comparison**

    - Return `True` if `value(firstWord) + value(secondWord) == value(targetWord)`
    - Return `False` otherwise

    &nbsp;

    The positional value calculation (`result * 10 + digit`) works because each new digit shifts existing digits left (multiplying by 10) before adding the new digit in the ones place.

  common_pitfalls:
    - title: Adding Letter Values Instead of Concatenating
      description: |
        A common mistake is to add up the individual letter values rather than treating them as positional digits.

        For example, with `"acb"`:
        - **Wrong**: `0 + 2 + 1 = 3`
        - **Correct**: `"021"` → `21`

        The problem explicitly states we **concatenate** letter values to form a string, then convert that string to an integer. The order and position of letters matters!
      wrong_approach: "Sum individual letter values"
      correct_approach: "Concatenate digits positionally"

    - title: String Concatenation Performance
      description: |
        While you could build the number as a string (concatenating digit characters) and then convert with `int()`, this is less efficient than the mathematical approach.

        Building numbers mathematically with `result = result * 10 + digit` avoids string allocation and is more performant, though with the small input constraints (`length <= 8`) both approaches work fine.
      wrong_approach: "String concatenation then int()"
      correct_approach: "Mathematical digit accumulation"

    - title: Forgetting Leading Zeros
      description: |
        Words like `"aaa"` convert to `"000"` which equals `0`, not `000`. This is handled automatically when we build the number mathematically or when Python's `int()` parses the string.

        The third example tests this edge case explicitly — make sure your solution handles words that convert to zero correctly.

  key_takeaways:
    - "**Character-to-digit mapping**: `ord(char) - ord('a')` is a common pattern for converting lowercase letters to numeric values"
    - "**Positional number building**: `result = result * 10 + digit` builds a number digit-by-digit from left to right"
    - "**Read carefully**: The problem describes concatenation, not addition — understanding the problem statement precisely is crucial"
    - "**Constraint awareness**: Letters limited to `'a'`-`'j'` ensures single digits, making the conversion straightforward"

  time_complexity: "O(n) where n is the total length of all three strings. We traverse each string exactly once to compute its numerical value."
  space_complexity: "O(1). We only use a constant number of integer variables regardless of input size."

solutions:
  - approach_name: Mathematical Conversion
    is_optimal: true
    code: |
      def is_sum_equal(first_word: str, second_word: str, target_word: str) -> bool:
          def word_to_value(word: str) -> int:
              """Convert a word to its numerical value."""
              result = 0
              for char in word:
                  # Each letter's value is its position: 'a'=0, 'b'=1, etc.
                  digit = ord(char) - ord('a')
                  # Shift existing digits left and add new digit
                  result = result * 10 + digit
              return result

          # Convert all words and check if sum equals target
          first_value = word_to_value(first_word)
          second_value = word_to_value(second_word)
          target_value = word_to_value(target_word)

          return first_value + second_value == target_value
    explanation: |
      **Time Complexity:** O(n) — We iterate through each character once.

      **Space Complexity:** O(1) — Only integer variables used.

      We convert each word to a number by processing characters left to right, treating each letter as a digit. The formula `result * 10 + digit` shifts existing digits left and places the new digit in the ones position.

  - approach_name: String Concatenation
    is_optimal: false
    code: |
      def is_sum_equal(first_word: str, second_word: str, target_word: str) -> bool:
          def word_to_value(word: str) -> int:
              """Convert a word to its numerical value using string concatenation."""
              # Build string of digits
              digit_string = ""
              for char in word:
                  digit = ord(char) - ord('a')
                  digit_string += str(digit)
              # Convert concatenated string to integer
              return int(digit_string)

          first_value = word_to_value(first_word)
          second_value = word_to_value(second_word)
          target_value = word_to_value(target_word)

          return first_value + second_value == target_value
    explanation: |
      **Time Complexity:** O(n) — Iterating through characters, though string concatenation has hidden costs.

      **Space Complexity:** O(n) — We create intermediate strings.

      This approach explicitly builds the digit string before converting to an integer. While conceptually matching the problem description, it's less efficient due to string allocation. With the small input size constraint (max 8 characters), the difference is negligible in practice.
