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 integer21
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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code