Check If Word Equals Summation of Two Words

Easy
523.

Check If Word Equals Summation of Two Words

Check If Word Equals Summation of Two Words is an easy coding problem on strings and math, asked in Amazon and TCS interviews.

The letters a through j stand for the digits 0 through 9. The numerical value of a word is obtained by replacing each letter with its digit and reading the result as a base-10 number — so "acb" becomes 021, which is 21.

Given three such words, return true if the value of firstWord plus the value of secondWord equals the value of targetWord.

Example 1

Input
firstWord = "acb", secondWord = "cba", targetWord = "cdb"
Output
true
Explanation
21 + 210 = 231.

Example 2

Input
firstWord = "aaa", secondWord = "a", targetWord = "aab"
Output
false
Explanation
0 + 0 is 0, not 1.

Example 3

Input
firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
Output
true
Explanation
0 + 0 = 0, and "aaaa" is also 0.

Constraints

  • 1 <= firstWord.length, secondWord.length, targetWord.length <= 8
  • All three consist of letters from 'a' to 'j'.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} firstWord
 * @param {string} secondWord
 * @param {string} targetWord
 * @return {boolean}
 */
var isSumEqual = function(firstWord, secondWord, targetWord) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Check If Word Equals Summation of Two Words — Solution

Check If Word Equals Summation of Two Words solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Check If Word Equals Summation of Two Words is an easy coding problem on strings and math, asked in Amazon and TCS interviews.

  • Difficulty: Easy
  • Topics: Strings, Math
  • Asked at: Amazon, TCS
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

The letters a through j stand for the digits 0 through 9. The numerical value of a word is obtained by replacing each letter with its digit and reading the result as a base-10 number — so "acb" becomes 021, which is 21.

Given three such words, return true if the value of firstWord plus the value of secondWord equals the value of targetWord.

Example 1

Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb"
Output: true
Explanation: 21 + 210 = 231.

Example 2

Input: firstWord = "aaa", secondWord = "a", targetWord = "aab"
Output: false
Explanation: 0 + 0 is 0, not 1.

Example 3

Input: firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
Output: true
Explanation: 0 + 0 = 0, and "aaaa" is also 0.

Constraints

  • 1 <= firstWord.length, secondWord.length, targetWord.length <= 8
  • All three consist of letters from 'a' to 'j'.

Hints for Check If Word Equals Summation of Two Words

  1. Hint 1

    Map each letter to c - 'a' to get its digit.

  2. Hint 2

    Build the number with the usual n = n * 10 + digit accumulation.

  3. Hint 3

    Leading zeros are fine — "aaa" is simply 0.

How to solve Check If Word Equals Summation of Two Words

Decoding a word is ordinary base-10 assembly with a letter-to-digit shift, after which the check is plain integer arithmetic.

Approach

  1. Write a helper that folds a word into a number via n = n * 10 + (c - 'a').
  2. Apply it to all three words.
  3. Return whether the first two values sum to the third.

Why it works

The mapping is a bijection between the letters a-j and the digits 0-9, so decoding then comparing is exactly the stated condition. With at most 8 letters the largest value is 99,999,999, which fits comfortably in 32 bits.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Concatenating the digit characters and comparing strings fails on leading zeros — "aab" (1) and "ab" (1) are equal as numbers.
  • Subtracting '0' instead of 'a' produces nonsense.

Reference solution

Python

def isSumEqual(firstWord: str, secondWord: str, targetWord: str) -> bool:
    def val(w):
        n = 0
        for c in w:
            n = n * 10 + (ord(c) - 97)
        return n
    return val(firstWord) + val(secondWord) == val(targetWord)

Java

public static boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
    return wordValueSum(firstWord) + wordValueSum(secondWord) == wordValueSum(targetWord);
}

static long wordValueSum(String w) {
    long n = 0;
    for (int i = 0; i < w.length(); i++) n = n * 10 + (w.charAt(i) - 'a');
    return n;
}

C++

long long wordValueSum(const string& w) {
    long long n = 0;
    for (char c : w) n = n * 10 + (c - 'a');
    return n;
}

bool isSumEqual(string firstWord, string secondWord, string targetWord) {
    return wordValueSum(firstWord) + wordValueSum(secondWord) == wordValueSum(targetWord);
}

JavaScript

var isSumEqual = function(firstWord, secondWord, targetWord) {
    function val(w) {
        let n = 0;
        for (let i = 0; i < w.length; i++) n = n * 10 + (w.charCodeAt(i) - 97);
        return n;
    }
    return val(firstWord) + val(secondWord) === val(targetWord);
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings