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