Under construction

Still being built. Things may change

Cryptarithm?!

Time limit1sMemory limit128 MB

Summary
Given three letter strings A+B=C, decide whether some assignment of distinct digits to letters makes the addition valid, columns up to 18 long.
Level

Hard9 of 10

Topics
Backtracking, Math, Combinatorics, Brute force
Solved
No attempts yet

Problem

Cryptarithm is a mathematical puzzle in which each digit of a calculation is replaced by a letter, and the solver must recover which digit each letter stands for.

A well known instance is SEND+MORE=MONEYSEND+MORE=MONEY.

  SEND
+ MORE
------
 MONEY

Setting S=9S=9, E=5E=5, N=6N=6, D=7D=7, M=1M=1, O=0O=0, R=8R=8, Y=2Y=2 satisfies the equation.

  9567
+ 1085
------
 10652

Given a cryptarithm A+B=CA+B=C written with three words, determine whether any digit assignment satisfies it.

The same letter always maps to the same digit, and different letters map to different digits. Numbers may start with 00.

Input

Three words AA, BB, CC are given, separated by spaces. They state that adding the first word and the second word yields the third word.

Each word consists only of uppercase English letters with no spaces, and each word is at most 1818 characters long.

Output

Print YESYES on one line if any satisfying assignment exists, and NONO otherwise.

Hint

There can be many solutions, only existence matters. The input SUNSUN FUNFUN SWIMSWIM admits 4949 solutions, including 067+867=0934067+867=0934 and 167+867=1034167+867=1034.

Examples2

  1. Example 1

    Input
    SUN FUN SWIM
    
    Expected output
    YES
    
  2. Example 2

    Input
    P P AP
    
    Expected output
    NO