LeetCode Problem Workspace

Check if Word Equals Summation of Two Words

Check if the sum of two word values equals a target word's value by converting letters to their numeric representations.

category

1

Topics

code_blocks

8

Code langs

hub

3

Related

Practice Focus

Easy · String-driven solution strategy

bolt

Answer-first summary

Check if the sum of two word values equals a target word's value by converting letters to their numeric representations.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for String-driven solution strategy

Try AiBox Copilotarrow_forward

This problem requires you to determine if the sum of two words' numeric values equals a target word's value. You convert each letter to its numeric value based on its position in the alphabet ('a' = 0, 'b' = 1, ..., 'j' = 9) and compare the sums. An effective solution focuses on accurately converting strings and performing the arithmetic.

Problem Statement

You are given three strings: firstWord, secondWord, and targetWord. Each word consists of lowercase English letters from 'a' to 'j'.

To solve this, convert each word into its corresponding numerical value, where each character is replaced by its position in the alphabet ('a' = 0, 'b' = 1, ..., 'j' = 9). You then check if the sum of the first two words' values equals the third word's value.

Examples

Example 1

Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb"

Output: true

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

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

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 <= 8
  • firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive.

Solution Approach

Convert Words to Numeric Values

Each letter in the words is converted into its numeric value. For example, 'a' becomes 0, 'b' becomes 1, and so on up to 'j'. This is achieved by iterating through the characters of each word and applying the appropriate transformation.

Sum the Values of the First Two Words

Once the words are converted into numeric values, the next step is to sum the values of the first two words. This involves simple arithmetic after the conversion process.

Compare with Target Word

Finally, compare the sum of the first two word values with the numeric value of the target word. If the sum matches the target word's value, return true; otherwise, return false.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity is O(n), where n is the length of the longest word since each word is processed once. The space complexity is O(1) since only integer values are stored, with no additional data structures.

What Interviewers Usually Probe

  • Evaluate if the candidate correctly implements the conversion of characters to numbers and ensures that the sum of the two words is compared to the target.
  • Look for understanding in string manipulation and handling of basic arithmetic operations.
  • Assess if the candidate is aware of edge cases, such as varying word lengths and the correct handling of smaller words.

Common Pitfalls or Variants

Common pitfalls

  • Incorrect character-to-number conversion, such as mapping characters beyond 'a' to 'j'.
  • Failing to account for the word length constraints, which could lead to incorrect calculations.
  • Mistakes in arithmetic when summing the values or comparing the results.

Follow-up variants

  • Modify the problem by allowing uppercase letters (A-Z) or extending the word length constraint beyond 8.
  • Include additional operations, such as subtracting or multiplying the values of the words.
  • Challenge with varying the character set to include characters beyond 'a' to 'j'.

FAQ

What is the approach for solving the Check if Word Equals Summation of Two Words problem?

The approach is to convert each word into its numeric value, sum the values of the first two words, and then compare the sum with the target word's value.

How do I convert a word into a numeric value in this problem?

Each character in the word is replaced by its position in the alphabet (e.g., 'a' = 0, 'b' = 1, ..., 'j' = 9), and the resulting digits form a number.

Why is the time complexity of this solution O(n)?

The time complexity is O(n) because we process each character of the words once, where n is the length of the longest word.

Can this problem be solved without converting the characters to numeric values?

No, the problem specifically requires converting the characters into numeric values to perform the necessary arithmetic and comparison.

What are the constraints on the word lengths and characters in this problem?

The word lengths are between 1 and 8 characters, and the characters are restricted to lowercase letters from 'a' to 'j'.

terminal

Solution

Solution 1: String to Number

We define a function $\textit{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.

1
2
3
4
5
6
7
8
9
10
class Solution:
    def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
        def f(s: str) -> int:
            ans, a = 0, ord("a")
            for c in map(ord, s):
                x = c - a
                ans = ans * 10 + x
            return ans

        return f(firstWord) + f(secondWord) == f(targetWord)
Check if Word Equals Summation of Two Words Solution: String-driven solution strategy | LeetCode #1880 Easy