Cryptarithmetic Solver - Problem

A cryptarithmetic puzzle is a mathematical equation where letters represent unique digits (0-9). Your task is to find the digit assignment that makes the equation valid.

Given a cryptarithmetic equation in the form WORD1 + WORD2 = RESULT, return a dictionary/map showing which digit each letter represents. If no valid solution exists, return an empty dictionary.

Rules:

  • Each letter represents a unique digit (0-9)
  • Leading letters cannot be 0 (e.g., in SEND, S ≠ 0)
  • The equation must be mathematically correct

Input & Output

Example 1 — Classic SEND + MORE = MONEY
$ Input: equation = "SEND + MORE = MONEY"
Output: {"S":9,"E":5,"N":6,"D":7,"M":1,"O":0,"R":8,"Y":2}
💡 Note: When S=9, E=5, N=6, D=7, M=1, O=0, R=8, Y=2, we get 9567 + 1085 = 10652, which is mathematically correct
Example 2 — Simple TWO + TWO = FOUR
$ Input: equation = "TWO + TWO = FOUR"
Output: {"T":7,"W":3,"O":4,"F":1,"U":9,"R":8}
💡 Note: When T=7, W=3, O=4, F=1, U=9, R=8, we get 734 + 734 = 1468, which satisfies the equation
Example 3 — No Solution Case
$ Input: equation = "ABC + DEF = GHI"
Output: {}
💡 Note: No valid digit assignment exists where a 3-digit + 3-digit = 3-digit with all different letters

Constraints

  • Each letter represents a unique digit (0-9)
  • Leading letters cannot be 0
  • Equation contains 2-10 unique letters
  • Input format: "WORD1 + WORD2 = RESULT"

Visualization

Tap to expand
INPUT PUZZLEALGORITHM STEPSFINAL RESULTSEND + MORE = MONEYEach letter = unique digitLeading letters ≠ 0Unique Letters:S E N D M O R Y8 letters need digits 0-91Parse equation structure2Identify constraints3Try digit assignments4Backtrack if invalidConstraint Pruning:S≠0, M≠0 (leading zeros)All digits uniqueEarly terminationSolution Found!9567 + 1085 = 10652Digit Mapping:S = 9E = 5N = 6D = 7M = 1O = 0R = 8Y = 2Valid Solution ✓Key Insight:Smart backtracking with mathematical constraints eliminates 99% of impossiblecombinations, solving cryptarithmetic puzzles efficiently instead of brute force.TutorialsPoint - Cryptarithmetic Solver | Smart Backtracking
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
12.8K Views
Medium Frequency
~25 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen