1880. Check if Word Equals Summation of Two Words

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 get 21.

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.

 

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.

 

Constraints:

  • 1 <= firstWord.length, secondWord.length, targetWord.length <= 8
  • firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive.

Rust Solution

struct Solution;

trait NumVal {
    fn val(&self) -> i32;
}

impl NumVal for String {
    fn val(&self) -> i32 {
        let mut res = 0;
        for b in self.bytes() {
            let x = b - b'a';
            res *= 10;
            res += x as i32;
        }
        res
    }
}

impl Solution {
    fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool {
        first_word.val() + second_word.val() == target_word.val()
    }
}

#[test]
fn test() {
    let first_word = "acb".to_string();
    let second_word = "cba".to_string();
    let target_word = "cdb".to_string();
    let res = true;
    assert_eq!(
        Solution::is_sum_equal(first_word, second_word, target_word),
        res
    );
    let first_word = "aaa".to_string();
    let second_word = "a".to_string();
    let target_word = "aab".to_string();
    let res = false;
    assert_eq!(
        Solution::is_sum_equal(first_word, second_word, target_word),
        res
    );
    let first_word = "aaa".to_string();
    let second_word = "a".to_string();
    let target_word = "aaaa".to_string();
    let res = true;
    assert_eq!(
        Solution::is_sum_equal(first_word, second_word, target_word),
        res
    );
}

Having problems with this solution? Click here to submit an issue on github.