class Solution:
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
def word_to_number(word):
return int(''.join(str(ord(c) - ord('a')) for c in word))
return word_to_number(firstWord) + word_to_number(secondWord) == word_to_number(targetWord)
class Solution {
public:
int wordToNumber(string word) {
string numStr = "";
for (char c : word) {
numStr += to_string(c - 'a');
}
return stoi(numStr);
}
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
return wordToNumber(firstWord) + wordToNumber(secondWord) == wordToNumber(targetWord);
}
};
class Solution {
private int wordToNumber(String word) {
StringBuilder sb = new StringBuilder();
for (char c : word.toCharArray()) {
sb.append((int)(c - 'a'));
}
return Integer.parseInt(sb.toString());
}
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
return wordToNumber(firstWord) + wordToNumber(secondWord) == wordToNumber(targetWord);
}
}
var isSumEqual = function(firstWord, secondWord, targetWord) {
function wordToNumber(word) {
return parseInt([...word].map(c => c.charCodeAt(0) - 'a'.charCodeAt(0)).join(''));
}
return wordToNumber(firstWord) + wordToNumber(secondWord) === wordToNumber(targetWord);
};
Given three strings firstWord
, secondWord
, and targetWord
consisting of lowercase English letters, you are asked to check whether the sum of the "numerical values" of firstWord
and secondWord
equals the numerical value of targetWord
.
The "numerical value" of a word is defined by replacing each letter with its position in the alphabet starting from 0 ('a'
= 0, 'b'
= 1, ..., 'j'
= 9), then concatenating the numbers to form an integer. For example, "acb"
becomes "021"
which is 21
as an integer.
Your task is to determine if:
numericalValue(firstWord) + numericalValue(secondWord) == numericalValue(targetWord)
To solve this problem, we first need to understand how to convert a word into its numerical value as described. Each letter is mapped to a digit (with 'a'
as 0, 'b'
as 1, and so on), and the digits are concatenated to form a number. For instance, "abc"
becomes 012
, which is 12
as an integer.
The brute-force approach would involve converting each word manually, possibly using a loop for each character. However, since the mapping is straightforward and the word lengths are small, this is already efficient. There is no need for complex optimizations such as hash maps or memoization.
The key insight is that this is a simple transformation and comparison problem. Once we have a helper function or logic to convert a word to its number, we just check if the sum of the first two equals the third.
'a'
from the character to get its digit value (e.g., ord('c') - ord('a') = 2
).firstWord
, secondWord
, and targetWord
.true
; otherwise, return false
.This approach is direct and efficient because string manipulation and integer conversion are fast, and the input sizes are small.
Suppose we have:
firstWord = "acb"
secondWord = "cba"
targetWord = "cdb"
The function would return true
for this example.
Brute-force approach: Even the brute-force approach is linear in the length of the words, because we process each character once.
O(N)
, where N
is the total number of characters in all three words. Each character is processed exactly once.
O(N)
for storing the intermediate strings during conversion. If you optimize by building the number directly, it's O(1)
extra space.
Optimized Approach: The solution described is already optimal for this problem, given the simplicity of the transformation.
This problem boils down to a simple string-to-number transformation using a fixed mapping from letters to digits. The key insight is recognizing that each letter's value can be computed directly using ASCII arithmetic, and the concatenation forms the number. The approach is both intuitive and efficient, making it a great example of how understanding the problem's structure can lead to an elegant and straightforward solution.