Skip to main content
Back to problems
#1880
Easy Algorithms

Check if word equals summation of two words

String
75.2% acceptance
Feb 25, 2026
609
42
The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0). The numerical value of a string s is the concatenation of the letter values of each letter in s, converted into an integer. Given three strings firstWord, secondWord, and targetWord (consisting of 'a' through 'j'), return true if firstWord + secondWord == targetWord numerically.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool {
    let to_num = |s: &str| -> i64 {
      s.bytes().fold(0i64, |acc, b| acc * 10 + (b - b'a') as i64)
    };
    to_num(&first_word) + to_num(&second_word) == to_num(&target_word)
  }
}