Problem Statement
A skew decimal numeral is a non-empty string of digits chosen from the set {'0'-'9','X'}. The digits '0'-'9' have their usual meanings, and the digit 'X' means ten (all quotes for clarity only). Not all combinations of digits are valid. In particular,
- the leftmost digit must be non-zero, and
- no non-zero digit may appear to the right of an 'X'.
Skew decimal numerals uniquely represent the positive integers, meaning there is a one-to-one mapping between positive integers and skew decimal numerals. For example, the integer 1 is represented by the skew decimal numeral "1" (quotes for clarity only). For each positive integer N, the representation of N+1 can be derived from the representation of N as follows:
- If the representation of N contains an 'X', increment the digit to the left of the 'X' and replace the 'X' with '0' (if the 'X' is the leftmost digit, first insert a '0' to its left). [Examples: "89X" plus one is "8X0" and "X00" plus one is "1000".]
- Otherwise, increment the rightmost digit. [Example: "12309" plus one is "1230X".]
What makes skew decimal numerals interesting is that the representations of N and N+1 differ in at most two digits, as opposed to ordinary decimal numerals, where N and N+1 may differ in every digit (eg, 9999 and 10000).
Create a class SkewDecimal containing a method multiply
that takes two skew decimal numerals, num1 and num2 (each a
Definition
- Class:
- SkewDecimal
- Method:
- multiply
- Parameters:
- String, String
- Returns:
- String
- Method signature:
- String multiply(String num1, String num2)
- (be sure your method is public)
Constraints
- num1 and num2 will each contain between 1 and 9 characters, inclusive.
- Each character in num1 and num2 will be either a digit ('0'-'9') or an uppercase 'X' (quotes for clarity only).
- num1 and num2 will be valid skew decimal numerals (ie, leftmost digit is non-zero and no non-zero digits to the right of an 'X').
Examples
"X"
"2"
Returns: "19"
"X" is 10 and "2" is 2, so their product is 20. The skew decimal representation of 20 is "19".
"X00000000"
"X00000000"
Returns: "1111111108888888899"
Maximum possible values.
"3"
"38"
Returns: "111"
"X00"
"902"
Returns: "X00000"
"28"
"38"
Returns: "1108"
"56"
"19"
Returns: "109X"
"3"
"34"
Returns: "100"
"3" is 3, and "34" is 37, so their product is 111. The skew decimal representation of 111 is "100".
"10000"
"513X0"
Returns: "571084293"
"1"
"1"
Returns: "1"
"123456789"
"987654321"
Returns: "135480695680705891"
"99887766X"
"817354X00"
Returns: "907152942717366542"
"2"
"5"
Returns: "X"
"334"
"2702705"
Returns: "X00000000"
"2000"
"5000000"
Returns: "11109998895"
"2348761"
"11X00000"
Returns: "3131676886832X"
"1010101"
"293487"
Returns: "329386734962"
"555555555"
"7777777"
Returns: "4801093845198942"
"2"
"4500"
Returns: "8X00"
"2003"
"19991492"
Returns: "44481059915"
"235711"
"13171923"
Returns: "3449712754656"
"7"
"200"
Returns: "13X0"
"424738X00"
"195438812"
Returns: "9223387009552292X"
"1"
"1"
Returns: "1"
"987654321"
"987654321"
Returns: "1083845609890091310"
"424738X00"
"195438812"
Returns: "9223387009552292X"
"1"
"1"
Returns: "1"
"987654321"
"987654321"
Returns: "1083845609890091310"