Check if Word Equals Summation of Two Words

easy string math

Problem

The letter value of a character is its position in the alphabet, 0-indexed ('a'=0 … 'j'=9). The numerical value of a word made of letters 'a'..'j' is the number formed by concatenating its letter values, read as a base-10 number. Given firstWord, secondWord, and targetWord, return true if value(firstWord) + value(secondWord) == value(targetWord).

Inputfirst = "acb", second = "cba", target = "cdb"
Outputtrue
"acb"=021=21, "cba"=210, "cdb"=231; 21 + 210 = 231 = 231, so true.

def is_sum_equal(first_word, second_word, target_word):
    def value(w):
        v = 0
        for ch in w:
            v = v * 10 + (ord(ch) - ord("a"))
        return v
    return value(first_word) + value(second_word) == value(target_word)
function isSumEqual(firstWord, secondWord, targetWord) {
  function value(w) {
    let v = 0;
    for (const ch of w) {
      v = v * 10 + (ch.charCodeAt(0) - 97);
    }
    return v;
  }
  return value(firstWord) + value(secondWord) === value(targetWord);
}
class Solution {
    private int value(String w) {
        int v = 0;
        for (char ch : w.toCharArray())
            v = v * 10 + (ch - 'a');
        return v;
    }
    public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
        return value(firstWord) + value(secondWord) == value(targetWord);
    }
}
int value(string w) {
    int v = 0;
    for (char ch : w)
        v = v * 10 + (ch - 'a');
    return v;
}
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
    return value(firstWord) + value(secondWord) == value(targetWord);
}
Time: O(n) Space: O(1)