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 Checker INPUT Three Words to Compare: firstWord = "acb" a c b secondWord = "cba" c b a targetWord = "cdb" c d b Letter Value Mapping: a=0, b=1, c=2, d=3, ... e=4, f=5, g=6, h=7, i=8, j=9 ALGORITHM STEPS 1 Convert Letters to Values a->0, c->2, b->1 2 Concatenate Digits "acb" -> "021" = 21 3 Calculate All Values "cba" -> "210" = 210 "cdb" -> "231" = 231 4 Compare Summation 21 + 210 == 231 ? Mathematical Conversion: firstWord: 21 secondWord: 210 Sum: 231 Target: 231 FINAL RESULT 21 + 210 = 231 231 == 231 MATCH OK Output: true The sum of numerical values of firstWord and secondWord equals targetWord's value. 21 + 210 = 231 [OK] Key Insight: Each letter ('a' to 'j') maps to a digit (0 to 9). Concatenating these digits forms a number string, which is then converted to an integer. The problem uses only letters 'a'-'j' to ensure single-digit values. TutorialsPoint - Check if Word Equals Summation of Two Words | Mathematical Conversion Approach
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