File: check-if-word-equals-summation-of-two-words/solution.py

Date: 2026-06-06

Time: 15:44

check-if-word-equals-summation-of-two-words/solution.py

Purpose

This file solves LeetCode 1880. It contains both the solution and its tests in a single module. The problem defines a letter-to-digit mapping where 'a'=0, 'b'=1, ..., 'j'=9, converts each word into a number by concatenating those digits, and checks whether firstWord + secondWord == targetWord under that encoding.

Key Components

Solution.isSumEqual — The sole public method. Takes three strings restricted to 'a''j' and returns a boolean.

wordtonum (nested closure) — Converts a word to its numeric value. For each character, it computes ord(c) - ord('a') to get a digit 0–9, joins the digit strings, then parses the result as an integer. For example, "acb""021"21.

TestSolution — Seven test cases covering the LeetCode examples, single-character inputs, boundary values ('a' = 0, 'j' = 9), and an all-zeros case.

Patterns

Dependencies

Imports: Only unittest from the standard library — no external dependencies.

Imported by: The test_solution.py in this same directory imports from this file. The massive "Imported By" list in the prompt is misleading — those are other problems' test files importing unittest, not this module. The actual dependency graph for this file is trivial.

Flow

1. isSumEqual is called with three strings.

2. Each string passes through wordtonum: character → ord delta → string digit → join → int().

3. The two numeric results are summed and compared to the third via ==.

4. A single boolean is returned.

There are no loops beyond the generator expressions inside wordtonum. The entire computation is O(n) in total characters across all three words.

Invariants

Error Handling

None. The function trusts its inputs per the LeetCode contract. Empty strings would produce int("") which raises ValueError, but the problem guarantees 1 <= len(word) <= 8.