Difficulty: Easy | Acceptance: 75.30% | Paid: No Topics: String
The letter-value of a letter is its position in the alphabet starting from 0 (i.e., ‘a’ -> 0, ‘b’ -> 1, …, ‘j’ -> 9). A word is a string of lowercase letters.
Given two words, firstWord and secondWord, and a target word, targetWord, return true if the summation of the numerical values of firstWord and secondWord equals the numerical value of targetWord, or false otherwise.
- Examples
- Constraints
- Direct Integer Conversion
- String-based Addition
- Array-based Addition
Examples
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.
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.
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.
Direct Integer Conversion
Intuition Convert each word to its numerical value by mapping each character to its digit value, then compare the sum of the first two with the target.
Steps
- Create a helper function to convert a word to a number
- For each character, calculate its digit value (ord(ch) - ord(‘a’))
- Build the number by multiplying by 10 and adding the digit
- Sum firstWord and secondWord values and compare with targetWord
class Solution:
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
def word_to_num(word: str) -> int:
num = 0
for ch in word:
num = num * 10 + (ord(ch) - ord('a'))
return num
return word_to_num(firstWord) + word_to_num(secondWord) == word_to_num(targetWord)Complexity
- Time: O(n) where n is the maximum length of the words
- Space: O(1) for the numeric conversion
- Notes: Simple and efficient, uses long/long long to handle potential overflow
String-based Addition
Intuition Convert words to digit strings and perform addition digit by digit from right to left, handling carry, then compare with target string.
Steps
- Convert each word to its digit string representation
- Add the two digit strings from right to left with carry
- Build the result string and reverse it
- Compare with the target digit string
class Solution:
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
def word_to_str(word: str) -> str:
return ''.join(str(ord(ch) - ord('a')) for ch in word)
first = word_to_str(firstWord)
second = word_to_str(secondWord)
target = word_to_str(targetWord)
i, j = len(first) - 1, len(second) - 1
carry = 0
result = []
while i >= 0 or j >= 0 or carry:
d1 = int(first[i]) if i >= 0 else 0
d2 = int(second[j]) if j >= 0 else 0
total = d1 + d2 + carry
result.append(str(total % 10))
carry = total // 10
i -= 1
j -= 1
sum_str = ''.join(reversed(result))
return sum_str == targetComplexity
- Time: O(n) where n is the maximum length of the words
- Space: O(n) for storing digit strings and result
- Notes: Avoids integer overflow issues, more verbose but safer for very large numbers
Array-based Addition
Intuition Store digits in arrays instead of strings, perform addition on arrays, then compare the resulting array with target digits.
Steps
- Convert each word to an array of digit values
- Add the two digit arrays from right to left with carry
- Build the result array and reverse it
- Compare with the target digit array
class Solution:
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
def word_to_digits(word: str) -> list:
return [ord(ch) - ord('a') for ch in word]
first_digits = word_to_digits(firstWord)
second_digits = word_to_digits(secondWord)
target_digits = word_to_digits(targetWord)
i, j = len(first_digits) - 1, len(second_digits) - 1
carry = 0
result = []
while i >= 0 or j >= 0 or carry:
d1 = first_digits[i] if i >= 0 else 0
d2 = second_digits[j] if j >= 0 else 0
total = d1 + d2 + carry
result.append(total % 10)
carry = total // 10
i -= 1
j -= 1
result.reverse()
return result == target_digitsComplexity
- Time: O(n) where n is the maximum length of the words
- Space: O(n) for storing digit arrays and result
- Notes: Similar to string-based approach but works with numeric arrays directly