# Did you hear about alphametics? [duplicate]

The letters spell out actual words, but if you replace each letter with a digit from 0–9, it also “spells” an arithmetic equation. The trick is to figure out which letter maps to each digit. All the occurrences of each letter must map to the same digit, no digit can be repeated, and no “word” can start with the digit 0.

# Input

HAWAII + IDAHO + IOWA + OHIO = STATES


# Output

510199 + 98153 + 9301 + 3593 = 621246


# Input

I + LOVE + YOU = DORA


# Output

1 + 2784 + 975 = 3760


# Input

SEND + MORE = MONEY


# Output

9567 + 1085 = 10652


# Rules

• Standard loopholes apply.
• shortest code wins.

# Appendix

for other Inputs you gotta keep in mind you can only have 10 different characters(to assign values 0-9)

• @Adám you can but don’t have to handle spaces, your input is also fine
– 0x45
Commented Oct 17, 2017 at 10:59
• Also this had no winning criterion. I added code-golf. Commented Oct 17, 2017 at 11:06
• What inputs are acceptable? Can we take the input as e.g. [[[S,E,N,D],[M,O,R,E]],[M,O,N,E,Y]]? Commented Oct 17, 2017 at 11:07
• Also can we assume that such a mapping is possible? Commented Oct 17, 2017 at 11:08
• Will there only be upper case letters A-Z? Can we choose lower case if we want? Commented Oct 17, 2017 at 11:13

# JavaScript (ES7), 104 bytes

f=(s,o,c=s.match(/[A-Z]/))=>c?[...2**29+'4'].some(i=>o&1<<i?0:f(r=s.split(c).join(i),o|1<<i))&&r:eval(s)


### Demo

f=(s,o,c=s.match(/[A-Z]/))=>c?[...2**29+'4'].some(i=>o&1<<i?0:f(r=s.split(c).join(i),o|1<<i))&&r:eval(s)

console.log(f('I + LOVE + YOU == DORA'))

### Formatted and commented

f = (                               // f = recursive function taking:
s,                                //   s = input string
o,                                //   o = bitmask of used digits
c = s.match(/[A-Z]/)              //   c = next alphabetical character in s
) =>                                //       (either null or a single-element array)
c ?                               // if c is not null:
[...2**29 + '4'].some(i =>      //   for each character i of '5368709124'
o & 1 << i ?                  //     if this digit was already used:
0                           //       ignore this iteration
:                             //     else:
f(                          //       do a recursive call to f() with:
r = s.split(c).join(i),   //         all characters c replaced with i
o | 1 << i                //         the updated bitmask
)                           //       end of recursive call
) && r                          //   end of some(): if successful, return r
:                                 // else:
eval(s)                         //   evaluate this fully numerical expression

• 104, since == is allowed
– 0x45
Commented Oct 17, 2017 at 11:23