Skip to main content

Check if Word Equals Summation of Two Words - Solution & Explanation

EasyString17 min read
Practice this problem

Problem Statement

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.

 

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

Approach Overview

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 1: Brute Force Conversion and Comparison

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Mapping and Direct Calculation

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n + m + p), based on input length. Space Complexity: O(1), primarily using computation space.

Try this approach in the editor →

Approach 3: String to Number

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).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Conversion and ComparisonO(n)O(n)When clarity is preferred and using built-in string-to-integer conversion is acceptable
Mapping and Direct CalculationO(n)O(1)Best for interviews and optimized solutions with no intermediate strings

Video Solution

Check if Word Equals Summation of Two Words 🔥 | Leetcode 1880 | Contest-243Ayushi Sharma921 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check if Word Equals Summation of Two Words easy or hard?
Check if Word Equals Summation of Two Words is classified as an Easy problem on LeetCode with a high acceptance rate around 75%. It mainly tests understanding of character-to-digit mapping and basic string iteration.
Check if Word Equals Summation of Two Words Python/Java solution
In Python or Java, iterate through the characters of each word and compute value = value * 10 + (ord(c) - ord('a')) or (c - 'a'). Repeat for the three words and compare the resulting integers. The logic is identical across Python, Java, C++, and JavaScript.
How to solve Check if Word Equals Summation of Two Words in O(n)?
Traverse each word and build its numeric value using digit mapping from 'a'–'j' to 0–9. Update the number with value = value * 10 + (c - 'a') for every character. After computing the values for firstWord, secondWord, and targetWord, return whether the first two values sum to the third.
What is the best approach for Check if Word Equals Summation of Two Words?
The best approach is mapping characters to digits and constructing the number directly during iteration. For each character, compute digit = c - 'a' and update value = value * 10 + digit. Convert all three words this way and compare the sum. This runs in O(n) time with O(1) extra space.
Is Check if Word Equals Summation of Two Words asked at Google/Amazon/Meta?
Problems of this type appear in screening rounds at companies like Amazon and Google because they test string manipulation and numeric construction from characters. The difficulty is Easy, but interviewers expect a clean O(n) implementation without unnecessary conversions.
What data structure is used in Check if Word Equals Summation of Two Words?
No complex data structure is required. The solution relies mainly on string traversal and simple arithmetic operations. Character-to-digit conversion is done using ASCII arithmetic such as c - 'a'.
What is the time complexity of Check if Word Equals Summation of Two Words?
The time complexity is O(n), where n is the total number of characters across the three words. Each character is processed exactly once to compute its numeric contribution. No nested loops or additional passes are required.

Ready to solve this problem?

Practice Check if Word Equals Summation of Two Words with our built-in code editor and test cases.

Practice on FleetCode