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 <= 8All three consist of letters from 'a' to 'j'.
Hints for Check If Word Equals Summation of Two Words
Hint 1
Map each letter to
c - 'a'to get its digit.Hint 2
Build the number with the usual
n = n * 10 + digitaccumulation.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
- Write a helper that folds a word into a number via
n = n * 10 + (c - 'a'). - Apply it to all three words.
- 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