Watch 10 video solutions for Check if Word Equals Summation of Two Words, a easy level problem involving String. This walkthrough by Ayushi Sharma has 909 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
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.
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.
Constraints:
1 <= firstWord.length, secondWord.length, targetWord.length <= 8firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive.Problem Overview: Each lowercase character from 'a' to 'j' represents a digit from 0 to 9. Converting a word means replacing every character with its corresponding digit and interpreting the resulting string as a number. The task is to check whether the numeric value of firstWord plus secondWord equals the numeric value of targetWord.
Approach 1: Brute Force Conversion and Comparison (O(n) time, O(n) space)
The direct approach converts each word into its numeric representation using a mapping from characters to digits. Iterate through every character, compute digit = c - 'a', append that digit to a string (or buffer), and then convert the resulting string into an integer using a standard parsing function. After computing values for firstWord, secondWord, and targetWord, compare whether the first two sum to the third. This method is straightforward and mirrors the problem statement exactly. The extra space comes from building intermediate digit strings before parsing. Time complexity is O(n), where n is the total length of all words.
Approach 2: Mapping and Direct Calculation (O(n) time, O(1) space)
You can avoid intermediate strings by constructing the numeric value directly during traversal. Start with value = 0, then iterate through each character and update value = value * 10 + (c - 'a'). This simulates digit concatenation mathematically. Apply the same logic to all three words and check whether value(firstWord) + value(secondWord) == value(targetWord). This eliminates parsing overhead and reduces memory usage since only integers are stored. The time complexity remains O(n) because each character is processed once, but the space complexity drops to O(1).
This problem mainly tests basic manipulation of string characters and simple digit construction, which is common in simulation problems. The numeric construction step also mirrors patterns seen in math-based string conversions where characters represent digits.
Recommended for interviews: The mapping and direct calculation approach. It demonstrates that you understand how digit concatenation works without relying on string parsing utilities. The brute force version shows correct interpretation of the problem, but the constant-space calculation is cleaner and typically what interviewers expect for simple conversion tasks.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Conversion and Comparison | O(n) | O(n) | When clarity is preferred and using built-in string-to-integer conversion is acceptable |
| Mapping and Direct Calculation | O(n) | O(1) | Best for interviews and optimized solutions with no intermediate strings |