Check if Word Equals Summation of Two Words - Problem

Imagine you're working on a secret code system where letters have special numerical meanings! In this fascinating problem, each letter from 'a' to 'j' represents a digit from 0 to 9 respectively.

Here's how the encoding works:

  • 'a' → 0, 'b' → 1, 'c' → 2, ..., 'j' → 9
  • When you have a word like "acb", you convert each letter to its digit: a=0, c=2, b=1
  • Concatenate these digits to form "021", which becomes the integer 21

Your Mission: Given three encoded words (firstWord, secondWord, and targetWord), determine if the numerical values of the first two words add up to the numerical value of the target word.

Return true if firstWord + secondWord = targetWord, otherwise return false.

Input & Output

example_1.py — Basic Addition
$ Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb"
Output: true
💡 Note: Converting words to numbers: "acb" → 021 → 21, "cba" → 210 → 210, "cdb" → 231 → 231. Since 21 + 210 = 231, the answer is true.
example_2.py — Unequal Sum
$ Input: firstWord = "aaa", secondWord = "a", targetWord = "aab"
Output: false
💡 Note: Converting words: "aaa" → 000 → 0, "a" → 0 → 0, "aab" → 001 → 1. Since 0 + 0 = 0 ≠ 1, the answer is false.
example_3.py — Single Characters
$ Input: firstWord = "a", secondWord = "a", targetWord = "b"
Output: false
💡 Note: Converting: "a" → 0, "a" → 0, "b" → 1. Since 0 + 0 = 0 ≠ 1, the answer is false.

Constraints

  • 1 ≤ firstWord.length, secondWord.length, targetWord.length ≤ 8
  • firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive
  • No string has leading zeros when converted to a number

Visualization

Tap to expand
Letter-to-Number Cipher DecoderCipher Key:a→0 b→1 c→2 d→3 e→4 f→5 g→6 h→7 i→8 j→9Step 1: Decode Words"acb"a=0, c=2, b=1"cba"c=2, b=1, a=0"cdb"c=2, d=3, b=1Step 2: Form Numbers021= 21210= 210231= 231Step 3: Verify Equation21 + 210 = 231 ✓Result: TRUE
Understanding the Visualization
1
Map Letters to Digits
Each letter a-j corresponds to digits 0-9 respectively
2
Build Numbers
Convert each word by concatenating its digit values
3
Verify Equation
Check if the first two numbers sum to the third
Key Takeaway
🎯 Key Insight: Convert letters directly to digits using character arithmetic (char - 'a'), eliminating the need for complex lookup tables or mappings.
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 6
28.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen