Check if Word Equals Summation of Two Words
Check if Word Equals Summation of Two Words is LeetCode 1880. You are given three strings firstWord, secondWord, and targetWord, each consisting only of lowercase letters 'a' through 'j'. The numerical value of a word is formed by concatenating the positions of its letters, where a = 0, b = 1, ..., j = 9. Return true if the numerical value of firstWord plus the numerical value of secondWord equals the numerical value of targetWord.
Example: firstWord = "acb", secondWord = "cba", targetWord = "cdb". The letter positions give "acb" = 021 = 21, "cba" = 210, and "cdb" = 231. Since 21 + 210 = 231, the answer is true.
The approach is to convert each word into its integer value by treating each character as a digit. Because only the letters 'a' through 'j' appear, each character maps to exactly one digit from 0 to 9. You can compute the numerical value by iterating through the string and building the number with standard base-10 arithmetic: multiply the running total by 10 and add the next digit. Once all three words are converted, compare the sum of the first two against the third.
This avoids string concatenation entirely. Instead of building a digit string and parsing it, you accumulate the number directly in a single pass over each word.
The letters are restricted to 'a' through 'j', which map exactly to digits 0 through 9. This means each word is really just a decimal number in disguise. The conversion formula for each character is ord(ch) - ord('a'), the same pattern used in many character-to-index problems.
The code
def isSumEqual(firstWord: str, secondWord: str, targetWord: str) -> bool:
def word_value(word: str) -> int:
value = 0
for ch in word:
value = value * 10 + (ord(ch) - ord('a'))
return value
return word_value(firstWord) + word_value(secondWord) == word_value(targetWord)
The helper function word_value processes one word at a time. For each character, it shifts the current value left by one decimal place (multiply by 10) and adds the digit for that character. This is the same technique used to convert a string of digit characters into an integer without calling int().
The main function calls word_value on all three words and checks whether the sum of the first two equals the third. That single boolean comparison is the entire answer.
Visual walkthrough
Step 1: Convert firstWord to its numerical value
a=0, c=2, b=1"021"=21Each character's position in the alphabet (starting from a=0) becomes a digit. Concatenating "0", "2", "1" gives the string "021", which is the integer 21.
Step 2: Convert secondWord to its numerical value
c=2, b=1, a=0"210"=210Same process: c maps to 2, b maps to 1, a maps to 0. The concatenated string "210" is the integer 210.
Step 3: Convert targetWord to its numerical value
c=2, d=3, b=1"231"=231c maps to 2, d maps to 3, b maps to 1. The concatenated string "231" is the integer 231.
Step 4: Check if firstWord + secondWord equals targetWord
The sum of the first two numerical values matches the target numerical value, so the answer is true.
Each step converts one word by mapping its letters to digits and concatenating them into a number. The final step adds the first two values and compares the sum to the third. The logic is the same regardless of word length, and the restricted alphabet guarantees every letter produces a valid single digit.
Complexity analysis
| Metric | Value |
|---|---|
| Time | O(n) where n is the total length of all three words |
| Space | O(1) extra space |
The algorithm makes a single pass over each of the three words, so the total work is proportional to the sum of their lengths. No extra data structures are needed beyond a few integer variables, so space usage is constant.
Building blocks
Character-to-digit mapping
The expression ord(ch) - ord('a') converts a lowercase letter to its zero-based position in the alphabet. For this problem, only 'a' through 'j' appear, so the result is always a single digit from 0 to 9. This same mapping shows up whenever you need to treat characters as numeric indices, such as counting letter frequencies with an array or converting between characters and offsets.
Number construction from digits
Building a number digit by digit follows the pattern value = value * 10 + digit. Each iteration shifts the existing digits left by one place and appends the new digit on the right. This is the manual version of what int("231") does internally. It works for any base: replace 10 with the desired base. You will see this pattern in problems like Reverse Integer, String to Integer (atoi), and any problem where you parse a number from a character sequence.
Edge cases
- Single-character words.
firstWord = "a",secondWord = "a",targetWord = "a". Values are 0, 0, and 0. Since 0 + 0 = 0, returntrue. This tests that single-letter inputs work correctly. - Leading zeros.
firstWord = "aaa"has value 000 = 0. Thevalue * 10 + digitformula handles leading zeros naturally because multiplying 0 by 10 is still 0. - Maximum values. Words can be up to 8 characters long with letters up to
'j'(digit 9), so the largest possible value is 99999999. This fits comfortably in a 32-bit integer, so overflow is not a concern. - All same letters.
firstWord = "jjj"= 999,secondWord = "a"= 0,targetWord = "jjj"= 999. Since 999 + 0 = 999, returntrue. This confirms the boundary letter'j'maps to 9 correctly. - Sum does not match.
firstWord = "ab",secondWord = "ab",targetWord = "ab". Values are 1, 1, and 1. Since 1 + 1 = 2 and 2 does not equal 1, returnfalse.
From understanding to recall
This problem is one of the simpler LeetCode questions, but it tests a fundamental pattern: converting characters to numbers and building integers digit by digit. The ord(ch) - ord('a') mapping and the value = value * 10 + digit accumulator are building blocks that appear in dozens of other problems. Drilling this solution until the conversion loop is automatic will pay off every time you encounter character-to-integer transformations under time pressure.
Related posts
- Add Strings - Another problem about converting between string and numeric representations
- Backspace String Compare - String comparison with a transformation step
- Buddy Strings - Another string equality check with a twist