Statistics

Problem Statement for "WordMath"

Problem Statement

A common task for math students is to solve 'word math' questions, where each distinct letter represents a distinct digit.
Given a list of word math numbers to add, your task is to calculate the greatest possible sum of those numbers.

For example, given the expression:
    TOP
+ CODER
the maximum sum possible by substituting numbers is:
    783
+ 98654
-------
  99437
with C = 9, D = 6, E = 5, O = 8, P = 3, R = 4 and T = 7.
Please note that, for this question, word math numbers are allowed to start with a zero.

Definition

Class:
WordMath
Method:
maximumSum
Parameters:
String[]
Returns:
int
Method signature:
int maximumSum(String[] summands)
(be sure your method is public)

Notes

  • Different letters must represent different digits, identical letters must represent identical digits.
  • Not all digits in the result must appear in the summands.

Constraints

  • summands will contain between 1 and 10 elements, inclusive.
  • Each element of summands will contain between 1 and 8 characters, inclusive.
  • Each element of summands will contain only uppercase letters ('A'-'Z').
  • summands will contain at most 10 distinct letters.

Examples

  1. {"TOP", "CODER"}

    Returns: 99437

    The example given in the problem statement.

  2. {"AAA", "AAA"}

    Returns: 1998

    With only one letter, the maximum sum is obtained by setting A = 9.

  3. {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}

    Returns: 45

    Each letter is used once, and there are 10 different letters, so however the digits are distributed this must add up to 0 + 1 + ... + 8 + 9 = 45

  4. {"AB", "BA"}

    Returns: 187

  5. {"AEGILOPS"}

    Returns: 98765432

    alphabetic order

  6. {"SPOONFED"}

    Returns: 98776543

    reverse alphabetic order

  7. {"AAAAAAAA", "AAAAAAAA", "AAAAAAAA", "AAAAAAAA", "AAAAAAAA", "AAAAAAAA", "AAAAAAAA", "AAAAAAAA", "AAAAAAAA", "BBBBBBBB"}

    Returns: 988888879

  8. {"T"}

    Returns: 9

  9. {"TCHS","SRM","EIGHT"}

    Returns: 106595

  10. {"KENNST","MICH","NOCH"}

    Returns: 1001682

  11. {"SATOR","AREPO","TENET","OPERA","ROTAS"}

    Returns: 383545

  12. {"BUENA","SUERTE"}

    Returns: 1065178

  13. {"OMG","LOL","HAKZOR","ROFL"}

    Returns: 994845

  14. {"DO","PEOPLE","READ","THESE"}

    Returns: 1067585

  15. {"TWO","SEVEN","OFF","SUIT"}

    Returns: 109356

  16. {"ONE","TWO","THREE","FOUR","NINE","TEN"}

    Returns: 114733

  17. {"IF","IT","IS","TO","BE","IT","IS","UP","TO","ME"}

    Returns: 825

  18. {"CPP","JAVA","CSHARP","VB"}

    Returns: 995042

  19. {"ZBBBBBBB","YBBBBBBB","XBBBBBBB","WBBBBBBB","VBBBBBBB","UBBBBBBB","TBBBBBBB","SBBBBBBB","RBBBBBBB"}

    Returns: 450000000

    breaks those who assign 9 to a first digit

  20. {"READIEST","OPERATIC","TOPCODER","PREDICTS","RAPIDEST","ASTEROID","AIRSPEED"}

    Returns: 563639653

  21. {"XMQME", "EXXMMXQM", "EXQM", "MXXQMEM", "QXQE"}

    Returns: 106869079

    40 random cases

  22. {"NSLVU", "U", "U", "U", "VWLSIV", "BN"}

    Returns: 1062285

  23. {"XPCVC", "DOXPD", "QCV", "OCXPOPDX", "C", "ODQ", "XOQ", "XO"}

    Returns: 98908578

  24. {"LLRJLJ", "JVRLRRJR", "VJL", "RL"}

    Returns: 99454639

  25. {"YV", "YVV", "V", "VYVVY"}

    Returns: 99995

  26. {"CCCXCXXX", "XCXC", "CXXXCCC", "CCCXXCC"}

    Returns: 119886775

  27. {"KK", "QZOZWQ"}

    Returns: 987925

  28. {"PP", "IIIIIPPP", "I", "IPPII"}

    Returns: 100098884

  29. {"PELGI", "E", "I", "E", "GIEI", "PGIGP"}

    Returns: 204971

  30. {"YI", "Y", "YIY", "IIYIIY", "IYYIYYY", "I", "IIIIIY"}

    Returns: 11889888

  31. {"XLLLX", "XLXLLLLX", "LXL", "XLL", "XL"}

    Returns: 99089762

  32. {"I", "UYITIYIG", "G", "IUTUTUYY"}

    Returns: 187565775

  33. {"XX", "XXXXXX", "XXX", "X"}

    Returns: 1001106

  34. {"RFFXQ", "QX"}

    Returns: 98943

  35. {"PPPPPPP", "P", "PPPPPPPP", "PPPPPP", "PPPPP", "PPPPPPP"}

    Returns: 121100004

  36. {"KPPP", "PGGZG", "PZ", "P"}

    Returns: 106972

  37. {"RMMMAMRA", "AMF", "MMFFRRR", "EF"}

    Returns: 107765639

  38. {"FGFD", "GFZGSGSS", "GZGDFGZG", "FZZDDD", "ZSZZSS", "DG", "GSZS"}

    Returns: 198530096

  39. {"QQXQYQ", "XK"}

    Returns: 999065

  40. {"YYHJHUK", "HXMAKM", "OMKX", "KH", "YMHAYAXK", "JXKOMA", "AMAUJ", "MOXAH"}

    Returns: 110272991

  41. {"LL", "CLLLDAV", "D", "ULXXX", "XARXC", "XVUXD", "LLV"}

    Returns: 10108058

  42. {"ZAXQQ", "QIANZQN", "NNVANA", "VYZN", "ZQBX"}

    Returns: 10719863

  43. {"WCGPVWC", "CVC", "WWVCPVWP", "GWCVV", "VCVPPGVC", "GGCPWCV", "PGCVPPW", "WGVWCPCG"}

    Returns: 307909045

  44. {"NLYNR", "YRYA", "AYALAANY", "NRLYRRRA", "N", "NRNLYR", "LANYR", "NNAR"}

    Returns: 185577669

  45. {"BBBBBBBB", "BBB", "BBBBBBBB", "B", "BBBBBB", "BBBBBBB", "BBBBBB"}

    Returns: 212001003

  46. {"KKK", "KK", "KKK"}

    Returns: 2097

  47. {"NIZYDMON", "ZODG", "DMYYII", "DNNMNDZY", "NONMIZ", "ZIO", "OGN"}

    Returns: 189409840

  48. {"QHQ", "E", "VVEVDT"}

    Returns: 999729

  49. {"HD", "GDGED", "D", "DHDTHH", "TD"}

    Returns: 1069693

  50. {"BOLO", "CL", "LJTLIB"}

    Returns: 994581

  51. {"WXWNGM", "WG", "MGNNM", "MANZ", "W", "XGX", "F"}

    Returns: 1075453

  52. {"JWWRWO"}

    Returns: 988786

  53. {"EEEOEOE", "OOEEE", "EEOOE", "OEEO", "EOE", "OEEEEOE", "OOOEO", "EE", "E"}

    Returns: 19286859

  54. {"MXMMXN", "XNNNXMXM", "NXXX", "XM", "NNMNM", "NN", "MXM"}

    Returns: 99786363

  55. {"ZBBZRR"}

    Returns: 988977

  56. {"OUDU", "UOGO", "OO"}

    Returns: 18946

  57. {"PKNDYK", "GDKYYDU", "K", "PDGU", "PUYGUY", "NQPYK", "GYKY"}

    Returns: 11544052

  58. {"W", "WWW", "W", "WRWWWRRR", "RWWR", "RWW", "RRWRRWW", "RRR"}

    Returns: 107910589

  59. {"ACII", "TATTRRGA", "TRMAC", "CMGTM", "R", "MTRAI", "MRTM", "MRC"}

    Returns: 99255405

  60. {"NNNUUGOW", "BNJWBJW", "UW", "NWNNWGNB", "O", "UPOWGWU", "BWPJJOJJ"}

    Returns: 300993072

  61. {"READIEST", "OPERATIC", "TOPCODER", "PREDICTS", "RAPIDEST", "ASTEROID", "AIRSPEED" }

    Returns: 563639653

  62. {"ABCDEF", "FEDCBA", "DCABEF", "EDCBAF", "BDECAF", "FFABCD" }

    Returns: 4856789

  63. {"AAAAAAAA", "BBBBBBBB", "CCCCCCC", "DDDDDDDD", "EEEEEEEE", "FFFFFFFF", "GGGGGGGG", "HHHHHHHH", "IIIIIIII", "KKKKKKKK" }

    Returns: 499999995

  64. {"HDIDGIJA", "CFGDCDDC", "EHDBIBDJ", "FHHDBJDE", "DEHHFIBG", "HFJGDBEF", "FFCEBIBE", "GFEBCIJJ", "IACJAHFA", "HADBCEDD" }

    Returns: 753052982


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: