Check if Word Equals Summation of Two Words - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb"
Output: true
💡 Note: acb → "021" → 21, cba → "210" → 210, cdb → "231" → 231. Since 21 + 210 = 231, return true.
Example 2 — False Case
$ Input: firstWord = "aaa", secondWord = "a", targetWord = "aab"
Output: false
💡 Note: aaa → "000" → 0, a → "0" → 0, aab → "001" → 1. Since 0 + 0 = 0 ≠ 1, return false.
Example 3 — Single Characters
$ Input: firstWord = "a", secondWord = "b", targetWord = "c"
Output: false
💡 Note: a → "0" → 0, b → "1" → 1, c → "2" → 2. Since 0 + 1 = 1 ≠ 2, return false.

Constraints

  • 1 ≤ firstWord.length, secondWord.length, targetWord.length ≤ 8
  • firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j'

Visualization

Tap to expand
Word Summation Problem OverviewfirstWord: "acb"a=0, c=2, b=1 → "021" → 21secondWord: "cba"c=2, b=1, a=0 → "210" → 210targetWord: "cdb"c=2, d=3, b=1 → "231" → 231Convert each word to number using letter positions (a=0, b=1, ...)Check: 21 + 210 = 231 ✓Output: true
Understanding the Visualization
1
Input
Three words: firstWord, secondWord, targetWord
2
Convert
Convert each word to numerical value using letter positions
3
Check Sum
Return true if first + second equals target
Key Takeaway
🎯 Key Insight: Convert letters to digit positions, concatenate to form numbers, then verify if sum equation holds
Asked in
Google 15 Microsoft 12
25.0K Views
Medium Frequency
~15 min Avg. Time
890 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