Problem Statement
The base of a number system is the number of different values each digit can represent. For example, in base-2 (binary), there are 2 values each digit can take: 0 and 1. In base-10, a digit can take values 0 through 9, inclusive. Sometimes, it is difficult to determine which base a numerical expression is in. An equation is valid for a given base if all of the digits are less than the base, and the numerical meaning of the equation is correct. For example, "1+1=10" for base-10 satisfies the rule that the digits are all less than 10, but 1+1 = 2 in base-10, so the equation is not correct for base-10.
If we assume that the characters '0'-'9' represent the values 0 - 9, and the characters 'A'-'J' represent the values 10 - 19, then we can represent numbers with a base up to 20. The equation will be in the following form:
<num>+<num>=<num>
Where each <num> is a
Given an equation as defined above, you should return which bases in the range of 2 to 20, inclusive, are valid for the equation. Return the bases in a
Definition
- Class:
- BaseMystery
- Method:
- getBase
- Parameters:
- String
- Returns:
- int[]
- Method signature:
- int[] getBase(String equation)
- (be sure your method is public)
Constraints
- equation will be of the form "
+ = " - Each
in equation will have between 1 and 5 characters, inclusive. - Each
in equation will consist only of numeric digits '0' - '9' and capital letters 'A' - 'J', and will not have extra leading zeros.
Examples
"1+1=2"
Returns: { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
The only base which this does not work for is base-2. In base 2, 2 is represented by "10"
"1+1=10"
Returns: { 2 }
The same equation valid for base 2.
"1+1=3"
Returns: { }
1+1 is never 3, no matter what base you are in.
"ABCD+211=B000"
Returns: { 14 }
In base-14, the digits are 0-9, A-D. Adding one to the ones column yields 10 base-14, which carries over to make the 10's column a 'D'. Adding 1 to that column yields 10 again, which carries and makes the 100's column a 'C'. Adding 2 to C yields 10 again, which adds 1 to the 1000's column, resulting in B000.
"ABCD+322=B000"
Returns: { 15 }
For base-15, we have increased the number required to wrap to 0 by 1.
"1+0=1"
Returns: { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
This is valid for all bases.
"GHIJ+1111=HJ00"
Returns: { 20 }
"1234+8765=9999"
Returns: { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
"1234+8766=10000"
Returns: { 10 }
"JJJJ+JJJJ=1JJJI"
Returns: { 20 }
"A9A9A+9A9A9=JJJJJ"
Returns: { 20 }
"I63+81G=1780"
Returns: { 19 }
"28H77+E8=7D37C"
Returns: { }
"E8+CDJE=CEE2"
Returns: { 20 }
"9+4FAIH=4FAJ6"
Returns: { 20 }
"F4+AH1=BC5"
Returns: { 20 }
"JB+11H9=F9F"
Returns: { }
"4CCF+4=4CD0"
Returns: { 19 }
"6+I3=I9"
Returns: { 19, 20 }
"6F+I=7D"
Returns: { 20 }
"8+A78=A85"
Returns: { 11 }
"JI+A14=EDGD"
Returns: { }
"26C+G20EA=G2323"
Returns: { 19 }
"7B+I23E3=I242E"
Returns: { 19 }
"I0HE+6=I0I1"
Returns: { 19 }
"9+D82=D8B"
Returns: { 14, 15, 16, 17, 18, 19, 20 }
"5+5499=549E"
Returns: { 15, 16, 17, 18, 19, 20 }
"3+1=4"
Returns: { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
"94529+58=94581"
Returns: { 16 }
"E+3D8=2J1A3"
Returns: { }
"BE+7FB=875"
Returns: { 20 }
"BI2B2+3I7DJ=2"
Returns: { }
"29+8=37"
Returns: { 10 }
"FI+64B7=6575"
Returns: { 20 }
"AJBD+3BF=B338"
Returns: { 20 }
"7H+JE6=71"
Returns: { }
"J+B21=BF"
Returns: { }
"27829+B=27831"
Returns: { 19 }
"1GGH+H36=2F14"
Returns: { 19 }
"IE30+I9H8=1I518"
Returns: { 19 }
"JH+I40=J3H"
Returns: { 20 }
"H1FI+73FHA=81HE9"
Returns: { 19 }
"ACC4+329C7=3D46B"
Returns: { 18 }
"JHEGI+JF1=JIEBJ"
Returns: { 20 }
"E+B21C=B227"
Returns: { 19 }
"G04+CCHJ=D8I3"
Returns: { 20 }
"34G74+G=1027"
Returns: { }
"9F+2BDA=2B2D"
Returns: { }
"C+603=60F"
Returns: { 16, 17, 18, 19, 20 }
"E+H7AD=H7B7"
Returns: { 20 }
"F248G+8AC=F2D2B"
Returns: { 17 }
"C3+4F6D=4G1G"
Returns: { 17 }
"G24+F=G32"
Returns: { 17 }
"DFF+E6=ED4"
Returns: { 17 }
"2JB2+AF58H=17"
Returns: { }
"9+23=32"
Returns: { 10 }
"B0+JAGF=JB7F"
Returns: { 20 }
"873+1GICD=1H80G"
Returns: { 19 }
"BFA2E+33C=BFD66"
Returns: { 20 }
"D+AE=BC"
Returns: { 15 }
"69+7F7=84G"
Returns: { 17 }
"1234+8765=9999"
Returns: { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
"0+0=0"
Returns: { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
"1+A=0"
Returns: { }
"1A+1=2"
Returns: { }
"1+1=2"
Returns: { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
"A+0=A"
Returns: { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
"9+3=2"
Returns: { }
"FFFF+1=FFFG"
Returns: { 17, 18, 19, 20 }
"A+2=11"
Returns: { 11 }