Problem Statement
You are given a digit string S and a positive integer integer X. We want to select a non-empty contiguous substring of S such that:
- The substring does not begin with the digit '0'.
- When you convert it to a number, its value is strictly greater than X.
Two ways of selecting a substring are different if they begin or end at a different index.
Compute and return the number of ways in which we can select a substring with the above properties.
Definition
- Class:
- DigitStringDiv2
- Method:
- count
- Parameters:
- String, int
- Returns:
- int
- Method signature:
- int count(String S, int X)
- (be sure your method is public)
Constraints
- S will contain between 1 and 9 characters, inclusive.
- Each character of S will be a digit ('0'-'9').
- X will be between 0 and 777,444,111, inclusive.
Examples
"0"
1
Returns: 0
"10"
9
Returns: 1
"471"
47
Returns: 2
We can select either the substring "471" or the substring "71".
"0"
777444111
Returns: 0
"000000000"
777444111
Returns: 0
"999999999"
777444111
Returns: 1
"00030"
708253
Returns: 0
"000589"
691668
Returns: 0
"0008409"
610805
Returns: 0
"00015496"
307266
Returns: 0
"000953561"
714547
Returns: 1
"45"
0
Returns: 3
"589"
0
Returns: 6
"8860"
0
Returns: 9
"20463"
0
Returns: 11
"322395"
0
Returns: 21
"782984137"
44633
Returns: 14
"731477481"
25708
Returns: 14
"350085176"
52176
Returns: 8
"203417351"
33007
Returns: 9
"080852803"
29582
Returns: 7
"970233950"
84993
Returns: 9
"289775158"
40240
Returns: 14
"528797331"
67759
Returns: 13
"658448500"
44534
Returns: 15
"672236426"
24987
Returns: 13
"00007000"
7
Returns: 3
"2222"
97
Returns: 3
We can select the entire string S ("2222"). We also have two different ways to select the string "222": either we choose the first three or the last three characters of S
"09"
1
Returns: 1
"009"
8
Returns: 1
"10100607"
6
Returns: 15
"02355653"
2
Returns: 27
"0011"
1
Returns: 1
"9"
0
Returns: 1
"009"
1
Returns: 1
"02"
1
Returns: 1
"1001177"
90
Returns: 8
"1099"
9
Returns: 4
"000000009"
0
Returns: 1
"0022220"
10000
Returns: 1