Two friends David and Rojer were preparing for their weekly class-test.
The are preparing for the math test, but because of continuously adding big integers and solving equations they got exhausted. They decided to take break and play a game. They play a game which will help them in both(for having fun and will also help to prepare for math test).
There are N words and they have to find RESULT with the help of these words (just like they have N integers and they have to find integer RESULT=sum of N integers) . Since they are playing this game for the first time! They are not too good in this. Help them in finding weather their RESULT is correct or not.   
NOTE:- Total number of unique characters in N words are not greater than 10. All input words and RESULT are in UPPER-CASE only!
Refer Here for how to add numbers : https://en.wikipedia.org/wiki/Verbal_arithmetic

-----Input:-----
- First line consist of an integer N (total number of words).
- Next N line contains words with which they have to find RESULT.
- Last line consist of the RESULT they found.

-----Output:-----
If RESULT is correct print true else print false.

-----Sample Input:-----
3

THIS

IS

TOO

FUNNY  

-----Sample Output:-----
true

-----Constraints-----
- $2 \leq N \leq 10$
- $1 \leq Length of The word \leq 10$
- $1 \leq Length of The Result \leq 11$

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '# Solution link: https://leetcode.com/problems/verbal-arithmetic-puzzle/discuss/939496/Python-Optimizations-to-beat-TLE-Top-Down-DP-(93)\r\n\r\n\r\n#dt = {} for i in x: dt[i] = dt.get(i,0)+1\r\nimport sys;input = sys.stdin.readline\r\ninp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]\r\n\r\nimport functools,collections\r\ndef isSolvable(words, result):\r\n\r\n    @functools.lru_cache(None)\r\n    def helper(i, total, nums):\r\n\r\n        if i == len(chars):\r\n            return total == 0\r\n\r\n        if i - 1 in checkpoints:\r\n            t = str(abs(total))[::-1]\r\n            for j in checkpoints[i-1]:\r\n                if (j < len(t)) and (t[j] != \'0\'):\r\n                    return False\r\n\r\n        for j in range(len(nums)):\r\n            if (nums[j] == 0) and (chars[i] not in not_zero) and helper(i+1, total, nums[:j]+nums[j+1:]):\r\n                return True\r\n            elif (nums[j] != 0) and helper(i+1, total + nums[j] * mult[chars[i]], nums[:j] + nums[j+1:]):\r\n                return True\r\n\r\n        return False\r\n\r\n    # 1. Check the lengths of each word and result\r\n    longest_word = len(max(words, key = len))\r\n    if (len(result) < longest_word) or (len(result) > longest_word + 1):\r\n        return False\r\n\r\n    # 2. Check if result is in words\r\n    if result in words:\r\n        return len(words) < 3 and all(word == result or len(word) == 1 for word in words)\r\n\r\n    # 3. Leading letters cannot be zero unless the length of the word is 1\r\n    not_zero = set((word[0] for word in words if len(word) > 1))\r\n    if len(result) > 1: not_zero.add(result[0])\r\n\r\n    # 4. Set of all letters\r\n    chars = set(result + \'\'.join(words))\r\n\r\n    # 5. Letters in words add to the total\r\n    mult = {char:0 for char in chars}\r\n    groups = collections.defaultdict(set)\r\n    for word in words:\r\n        for i,char in enumerate(reversed(word)):\r\n            mult[char] += 10**i\r\n            groups[i].add(char)\r\n\r\n    # 6. And letters in result subtract from the total\r\n    for i,char in enumerate(reversed(result)):\r\n        mult[char] -= 10**i\r\n        groups[i].add(char)\r\n\r\n    # 7. Letters that add and subtract the same amount can be any number, so ignore them.\r\n    chars = {char for char in chars if mult[char]}\r\n    for g in groups:\r\n        groups[g] = groups[g].intersection(chars)\r\n    chars = list(chars)\r\n\r\n    # 8. All letters that occur later in the word may affect letters ealrier in the word\r\n    for g in range(1, len(groups)):\r\n        groups[g] |= groups[g-1]\r\n    chars.sort(key = lambda c: min(g for g in range(len(groups)) if c in groups[g]))\r\n\r\n    # 9. Once a number has been assigned to all the letters in a group\r\n    #    the digit in total at position 10**i must be zero for a solution to exist\r\n    checkpoints = collections.defaultdict(list)\r\n    seen = set()\r\n    checked = set()\r\n    for i,char in enumerate(chars):\r\n        seen.add(char)\r\n        for g in groups:\r\n            if (g not in checked) and groups[g].issubset(seen):\r\n                checked.add(g)\r\n                checkpoints[i].append(g)\r\n\r\n    return helper(0, 0, tuple(range(10)))\r\n\r\nn = inp()\r\nx = [input().strip() for i in range(n)]\r\ny = input().strip()\r\nans = isSolvable(x,y)\r\nif ans:\r\n    print("true")\r\nelse:\r\n    print("false")\r\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 883484