Verbal Arithmetic Puzzle - Problem

Given an equation represented by words on the left side and the result on the right side.

You need to check if the equation is solvable under the following rules:

  • Each character is decoded as one digit (0-9)
  • No two characters can map to the same digit
  • Each word and result are decoded as numbers without leading zeros
  • Sum of numbers on the left side equals the number on the right side

Return true if the equation is solvable, otherwise return false.

Input & Output

Example 1 — Basic Solvable Case
$ Input: words = ["SEND", "MORE"], result = "MONEY"
Output: true
💡 Note: One valid assignment: S=9, E=5, N=6, D=7, M=1, O=0, R=8, Y=2. This gives 9567 + 1085 = 10652, which is valid.
Example 2 — Leading Zero Violation
$ Input: words = ["SIX", "SEVEN", "SEVEN"], result = "TWENTY"
Output: true
💡 Note: Multiple characters make this complex, but a valid assignment exists where no leading character maps to 0.
Example 3 — Impossible Case
$ Input: words = ["THIS", "IS"], result = "TOO"
Output: false
💡 Note: No digit assignment can make THIS + IS = TOO work while satisfying all constraints.

Constraints

  • 2 ≤ words.length ≤ 5
  • 1 ≤ words[i].length ≤ 7
  • 1 ≤ result.length ≤ 7
  • words[i] and result contain only uppercase English letters
  • The number of different characters used in the expression is at most 10

Visualization

Tap to expand
Verbal Arithmetic Puzzle INPUT words[] array: SEND MORE + result: MONEY Unique chars: S,E,N,D,M,O,R,Y Constraints: - Each char = one digit (0-9) - No two chars = same digit - No leading zeros ALGORITHM STEPS 1 Identify Characters Extract unique letters, track leading chars (S, M) 2 Backtrack + Assign Try digit 0-9 for each char, skip used digits 3 Validate Mapping Leading chars cannot be 0, check sum equation 4 Prune + Optimize Early termination if partial sum exceeds result Valid Mapping Found: S=9 E=5 N=6 D=7 M=1 O=0 R=8 Y=2 9567 + 1085 = 10652 SEND + MORE = MONEY FINAL RESULT true Solvable! Solution Verified: SEND + MORE MONEY 9567 + 1085 Output: true Equation is solvable with valid mapping [OK] Key Insight: Use backtracking with constraint propagation. Process columns right-to-left (like manual addition) to enable early pruning. Track carry values between columns. Leading characters (S, M) cannot be 0. Time Complexity: O(10!) in worst case, but pruning drastically reduces search space in practice. TutorialsPoint - Verbal Arithmetic Puzzle | Optimal Solution (Backtracking) Input: words = ["SEND", "MORE"], result = "MONEY" ---> Output: true
Asked in
Google 12 Amazon 8 Microsoft 6 Facebook 4
28.0K Views
Medium Frequency
~35 min Avg. Time
892 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