#1880

Check if Word Equals Summation of Two Words

newbie · 230 · lc easy +17 · premium · verified · 75.2% accepted · 609 likes · top 86%

Description

Map each letter to its 0-based position ('a' -> 0, 'b' -> 1, ..., 'j' -> 9). The numerical value of a word is formed by concatenating the position of each letter and reading the result as an integer.

Given three strings firstWord, secondWord, and targetWord (each using only 'a' through 'j'), return true if the sum of the numerical values of firstWord and secondWord equals the numerical value of targetWord.

Example 1:

Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb"
Output: true
Explanation:
The numerical value of firstWord is "acb" -> "021" -> 21.
The numerical value of secondWord is "cba" -> "210" -> 210.
The numerical value of targetWord is "cdb" -> "231" -> 231.
We return true because 21 + 210 == 231.

Example 2:

Input: firstWord = "aaa", secondWord = "a", targetWord = "aab"
Output: false
Explanation:
The numerical value of firstWord is "aaa" -> "000" -> 0.
The numerical value of secondWord is "a" -> "0" -> 0.
The numerical value of targetWord is "aab" -> "001" -> 1.
We return false because 0 + 0 != 1.

Example 3:

Input: firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
Output: true
Explanation:
The numerical value of firstWord is "aaa" -> "000" -> 0.
The numerical value of secondWord is "a" -> "0" -> 0.
The numerical value of targetWord is "aaaa" -> "0000" -> 0.
We return true because 0 + 0 == 0.

Code

1
2
3