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.
This approach directly translates each character of the input strings into its respective letter value, concatenates them to form a number (as a string), and then converts it into an integer. We do this separately for firstWord, secondWord, and targetWord, and finally compare the sum of the first two with the third.
Defines a helper function getNumericalValue that takes a string and converts it into its numerical representation by treating each character as a base-10 digit. The isSumEqual function then checks if the sum of the numerical values of firstWord and secondWord equals targetWord.
Time Complexity: O(n + m + p), where n, m, and p are the lengths of the given strings. Space Complexity: O(1), as we use a constant amount of space for computation.
This approach uses a mapping of characters to their numerical equivalents directly and computes the numerical values using mathematical transformations. This avoids concatenating strings and directly supports arithmetic operations on numerical values.
Directly computes the numerical value by iterating backwards through the string and converting each letter's value into its corresponding numerical power. This avoids building the full string yet translates directly to a usable number for comparison.
Time Complexity: O(n + m + p), based on input length. Space Complexity: O(1), primarily using computation space.
We define a function f(s) to calculate the numerical value of the string s. For each character c in the string s, we convert it to the corresponding number x, then concatenate x sequentially, and finally convert it to an integer.
Finally, we just need to check whether f(firstWord) + f(secondWord) equals f(targetWord).
The time complexity is O(L), where L is the sum of the lengths of all strings in the problem. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C
| Approach | Complexity |
|---|---|
| Brute Force Conversion and Comparison | Time Complexity: O(n + m + p), where n, m, and p are the lengths of the given strings. Space Complexity: O(1), as we use a constant amount of space for computation. |
| Mapping and Direct Calculation | Time Complexity: O(n + m + p), based on input length. Space Complexity: O(1), primarily using computation space. |
| String to Number | — |
| 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 |
Check if Word Equals Summation of Two Words 🔥 | Leetcode 1880 | Contest-243 • Ayushi Sharma • 909 views views
Watch 9 more video solutions →Practice Check if Word Equals Summation of Two Words with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor