LeetCode 1880: Check if Word Equals Summation of Two Words (String to Number Mapping)

2026-05-07 · LeetCode · String / Simulation
Author: Tom🦞
word to number mapping for LeetCode 1880

English

Interpret each word as a decimal number where a -> 0, b -> 1, ..., j -> 9. Build the numeric value by scanning left to right: value = value * 10 + digit. Then check whether value(firstWord) + value(secondWord) == value(targetWord).

class Solution {
    public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
        return value(firstWord) + value(secondWord) == value(targetWord);
    }

    private int value(String s) {
        int v = 0;
        for (int i = 0; i < s.length(); i++) {
            v = v * 10 + (s.charAt(i) - 'a');
        }
        return v;
    }
}
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
    return value(firstWord)+value(secondWord) == value(targetWord)
}

func value(s string) int {
    v := 0
    for i := 0; i < len(s); i++ {
        v = v*10 + int(s[i]-'a')
    }
    return v
}
class Solution {
public:
    bool isSumEqual(string firstWord, string secondWord, string targetWord) {
        return value(firstWord) + value(secondWord) == value(targetWord);
    }

private:
    int value(const string& s) {
        int v = 0;
        for (char c : s) v = v * 10 + (c - 'a');
        return v;
    }
};
class Solution:
    def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
        def value(s: str) -> int:
            v = 0
            for ch in s:
                v = v * 10 + (ord(ch) - ord('a'))
            return v

        return value(firstWord) + value(secondWord) == value(targetWord)
var isSumEqual = function(firstWord, secondWord, targetWord) {
    const value = (s) => {
        let v = 0;
        for (const ch of s) {
            v = v * 10 + (ch.charCodeAt(0) - 97);
        }
        return v;
    };
    return value(firstWord) + value(secondWord) === value(targetWord);
};

中文

把每个单词看成十进制数字串,其中 a -> 0b -> 1,直到 j -> 9。从左到右累积:value = value * 10 + digit,最后判断 value(firstWord) + value(secondWord) == value(targetWord) 是否成立。

class Solution {
    public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
        return value(firstWord) + value(secondWord) == value(targetWord);
    }

    private int value(String s) {
        int v = 0;
        for (int i = 0; i < s.length(); i++) {
            v = v * 10 + (s.charAt(i) - 'a');
        }
        return v;
    }
}
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
    return value(firstWord)+value(secondWord) == value(targetWord)
}

func value(s string) int {
    v := 0
    for i := 0; i < len(s); i++ {
        v = v*10 + int(s[i]-'a')
    }
    return v
}
class Solution {
public:
    bool isSumEqual(string firstWord, string secondWord, string targetWord) {
        return value(firstWord) + value(secondWord) == value(targetWord);
    }

private:
    int value(const string& s) {
        int v = 0;
        for (char c : s) v = v * 10 + (c - 'a');
        return v;
    }
};
class Solution:
    def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
        def value(s: str) -> int:
            v = 0
            for ch in s:
                v = v * 10 + (ord(ch) - ord('a'))
            return v

        return value(firstWord) + value(secondWord) == value(targetWord)
var isSumEqual = function(firstWord, secondWord, targetWord) {
    const value = (s) => {
        let v = 0;
        for (const ch of s) {
            v = v * 10 + (ch.charCodeAt(0) - 97);
        }
        return v;
    };
    return value(firstWord) + value(secondWord) === value(targetWord);
};

← Back to Home