Skip to problem
←

Solving Cryptarithmetic Puzzles

Hard 0 attempts

Assign a decimal digit to every letter so that the sum of the addends in words equals result.

  • Each letter always represents the same digit.
  • Different letters must represent different digits.
  • The first letter of every word must be nonzero. In this version, this also applies to one-letter words.
  • Return a letter-to-digit object, such as {"A":1,"B":2,"C":3} for A + B = C. Any valid assignment is accepted.
  • Return null if no assignment satisfies all rules.

For SEND + MORE = MONEY, one assignment gives 9567 + 1085 = 10652.

Words may contain up to 20 letters. Their numeric values can exceed JavaScript's safe integer range; use column arithmetic with carries or exact integer arithmetic rather than converting whole words to Number.

Function contract

Implement solveCryptarithmetic(words, result).

Output: Return an object mapping every letter to its integer digit, or null when no assignment exists. Any valid assignment is accepted; object key order does not matter.

Example 1

Input
words = ["SEND","MORE"]
result = "MONEY"
Output
{"S":9,"E":5,"N":6,"D":7,"M":1,"O":0,"R":8,"Y":2}

Example 2

Input
words = ["A","B"]
result = "AA"
Output
null
Constraints
  • 1 <= words.length <= 5
  • 1 <= words[i].length, result.length <= 20
  • All characters are uppercase English letters.
  • At most 10 distinct letters occur across all addends and the result.
  • The first letter of every word, including a one-letter word, must map to a non-zero digit.

Approach: Column-by-Column Backtracking

Work from the units column toward the left. Assign unused digits to the addend letters, then enforce the result digit and carry immediately. Repeated letters reuse their assigned digit. Reject zero for leading letters. This prunes invalid assignments before completing the whole mapping and avoids converting long words to JavaScript numbers.

javascript
function solveCryptarithmetic(words, result) {
  const letters = [...new Set(words.join('') + result)];
  if (letters.length > 10) return null;
  const leading = new Set([...words.map(w => w[0]), result[0]]);
  const assignment = {}, used = new Set();
  const width = Math.max(result.length, ...words.map(w => w.length));
  function column(col, row, sum) {
    if (col === width) return sum === 0;
    if (row < words.length) {
      const word = words[row], ch = word[word.length - 1 - col];
      if (!ch) return column(col, row + 1, sum);
      if (assignment[ch] !== undefined) return column(col, row + 1, sum + assignment[ch]);
      for (let d = leading.has(ch) ? 1 : 0; d <= 9; d++) {
        if (used.has(d)) continue;
        assignment[ch] = d; used.add(d);
        if (column(col, row + 1, sum + d)) return true;
        delete assignment[ch]; used.delete(d);
      }
      return false;
    }
    const ch = result[result.length - 1 - col];
    if (!ch) return sum === 0 && column(col + 1, 0, 0);
    const digit = sum % 10, carry = Math.floor(sum / 10);
    if (assignment[ch] !== undefined) return assignment[ch] === digit && column(col + 1, 0, carry);
    if (used.has(digit) || (digit === 0 && leading.has(ch))) return false;
    assignment[ch] = digit; used.add(digit);
    if (column(col + 1, 0, carry)) return true;
    delete assignment[ch]; used.delete(digit);
    return false;
  }
  return column(0, 0, 0) ? { ...assignment } : null;
}

Time Complexity: O(L × 10! / (10-U)!) as a loose upper bound, where U is the number of unique letters and L is the total input length

Space Complexity: O(L + U) for recursion and the assignment map

Loading discussion…

Saved in this browser only. Private to you.

JavaScript