# 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)

## marked as duplicate by Erik the Outgolfer, Mr. Xcoder, Peter Taylor code-golf StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Oct 17 '17 at 11:13

• @Adám you can but don’t have to handle spaces, your input is also fine – 0x45 Oct 17 '17 at 10:59
• Also this had no winning criterion. I added code-golf. – Mr. Xcoder Oct 17 '17 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]]? – Fatalize Oct 17 '17 at 11:07
• Also can we assume that such a mapping is possible? – Mr. Xcoder Oct 17 '17 at 11:08
• Will there only be upper case letters A-Z? Can we choose lower case if we want? – Stewie Griffin Oct 17 '17 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 Oct 17 '17 at 11:23