Problem
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 get21
.
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.
Examples
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.
Solution
Method 1 - Get numerical value
Here is the approach:
- Create a helper function to convert a word to its numerical value by:
- Calculating the letter value of each character.
- Concatenating these values into a string.
- Converting the string to an integer.
- Compute the numerical values of
firstWord
,secondWord
, andtargetWord
. - Check if the sum of the numerical values of
firstWord
andsecondWord
is equal to the numerical value oftargetWord
.
Code
Java
public class Solution {
private int getNumericalValue(String word) {
StringBuilder sb = new StringBuilder();
for (char c : word.toCharArray()) {
sb.append(c - 'a');
}
return Integer.parseInt(sb.toString());
}
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
int num1 = getNumericalValue(firstWord);
int num2 = getNumericalValue(secondWord);
int target = getNumericalValue(targetWord);
return num1 + num2 == target;
}
}
Python
class Solution:
def get_numerical_value(self, word: str) -> int:
num_str = ''.join(str(ord(c) - ord('a')) for c in word)
return int(num_str)
def is_sum_equal(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
num1 = self.get_numerical_value(firstWord)
num2 = self.get_numerical_value(secondWord)
target = self.get_numerical_value(targetWord)
return num1 + num2 == target
Complexity
- ⏰ Time complexity:
O(n)
wheren
is the length of the longest word amongfirstWord
,secondWord
, andtargetWord
. - 🧺 Space complexity:
O(1)
since we are only using a fixed amount of space for variables and not additional data structures.