Ctrl K

Letter Sum Cipher

Hard
You are given a puzzle: the words in addends are added together and their sum is the word total. Every word is written in uppercase English letters. Replace every letter with a digit from 0 to 9 so that: - the same letter always gets the same digit and different letters get different digits; - no word of two or more letters starts with zero (a one-letter word may be zero); - the numbers from addends add up to the number total. There are only ten digits, so a puzzle with more than ten distinct letters has no solution. Return True if such a replacement exists and False otherwise.
Example 1
addends=['TO', 'GO'], total='OUT' → True

T = 2, O = 1, G = 8, U = 0 works: 21 + 81 = 102.

Example 2
addends=['AB', 'AB'], total='AB' → False

AB + AB = AB forces AB = 0, but a two-digit number cannot start with zero.

Example 3
addends=['A', 'B'], total='A' → True

Take B = 0 and any other digit for A: a one-letter word may stand for zero.

Constraints
  • 2 ≤ addends.length ≤ 5
  • 1 ≤ addends[i].length, total.length ≤ 11
  • words consist of uppercase English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Way to Space the Words HardWords Hidden in a Letter Grid HardSigns Between the Digits HardShortest Unambiguous Shorthand HardMirror Word Blocks Hard
loading Python…
Input
addends=['TO', 'GO'], total='OUT'
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 150 tests on the server, hidden ones included.