Problem Statement
A string C matches B if we can change B into C by changing each '?' in B either to '0' or to '1'. Different occurrences of '?' may be changed to different digits. For example, C = "0101" matches B = "01??". Note that each character in C must be either '0' or '1', there cannot be any '?' remaining.
Consider all possible strings that match B. How many of these strings occur as a (contiguous) substring in A? Compute and return their number. Note that we only count each good string once, even if it has multiple occurrences in A.
Definition
- Class:
- NonDeterministicSubstring
- Method:
- ways
- Parameters:
- String, String
- Returns:
- long
- Method signature:
- long ways(String A, String B)
- (be sure your method is public)
Constraints
- A will contain between 1 and 50 characters, inclusive.
- B will contain between 1 and 50 characters, inclusive.
- Each character in A will be '0' or '1'.
- Each character in B will be '0', '1' or '?'.
Examples
"00010001"
"??"
Returns: 3
There are four strings that match B: the strings "00", "01", "10", and "11". Out of these, the string "11" does not occur in A as a substring. The other three do occur, hence the answer is 3.
"00000000"
"??0??0"
Returns: 1
There are 16 different strings that match B, but out of those only the string "000000" is a substring of A.
"001010101100010111010"
"???"
Returns: 8
Each of the 8 strings that match B occurs somewhere in A.
"00101"
"??????"
Returns: 0
Here, the length of B is greater than the length of A. Clearly, a string that matches this B cannot be a substring of this A.
"1101010101111010101011111111110001010101"
"???????????????????????????????????"
Returns: 6
"1100011000010000001011000010101110110"
"?011101??000?0"
Returns: 0
"0010111100001001111111010101001000"
"1?100???00??1???000"
Returns: 0
"01001001111011101100010010111111001111111110101110"
"10?1111"
Returns: 2
"1110110111001001110001001101000011011111101100001"
"0"
Returns: 1
"101101101010100101100100011111001100111010110"
"?10100?1?0110?00100?1???01??1100"
Returns: 0
"00100011100011000110111010011011011000001"
"11111?110010?001?1100?01001111110110"
Returns: 0
"0100000010111001010101"
"0110?1000"
Returns: 0
"11001111000001"
"10001011?10?"
Returns: 0
"1000001011111000000111101001101001111"
"010101?"
Returns: 0
"1111011111001010001111101000100111011110101011010"
"10111?11?110????010?0110??01100??0?1?0?1000000?"
Returns: 0
"11111110011100000110000101111010001110100110"
"0???01???????"
Returns: 3
"1111011000"
"0?1?????0"
Returns: 0
"1010011100001000101101010101"
"????????????????0??????"
Returns: 2
"11100000110110100000111100111100011110011101010101"
"????0?????10????????0"
Returns: 1
"111101011101010110000000110101100111110"
"10??1?0??1???1??"
Returns: 0
"111100001111000110001"
"?????????0????1??"
Returns: 0
"11010000111100001011100111101011000001111100010001"
"????"
Returns: 16
"01110000110001110001101111000011"
"???????????0?0??1?0"
Returns: 0
"1110011111100010011101101100010000101111110"
"1???????0?0??????0?????????1???????0??"
Returns: 0
"11000000001101100010000101000100011111000010000"
"???10????1"
Returns: 2
"1011111111110111101000001110101011100000010"
"??????1??"
Returns: 18
"001111010111011011100000101111111110"
"1?????1??????????????0??00?????????"
Returns: 0
"00111011111110110011010101110111001011110001101110"
"????1?????0"
Returns: 12
"110011011001011111110100011101100"
"?00?????0????1??1???????1???1?0??"
Returns: 0
"0010010011000001000000011001111000111000111"
"??0??1????00?"
Returns: 1
"000010011110111000"
"??0?1?????"
Returns: 3
"110101111101010000010100111110111010100"
"????????1????11???????????0????"
Returns: 0
"110101111110000001010100101011111"
"????0??????????????1??????"
Returns: 2
"011100101001010110010001011000011101"
"???0?1?????????????????????????????"
Returns: 1
"0011100011011000101010000101011010"
"0????????????????1?????"
Returns: 2
"00000000100"
"?????????"
Returns: 3
"0011001000100"
"????????"
Returns: 6
"000000000100001000000001000011000000000010000"
"??????0?????????????????????"
Returns: 15
"0100010000000000"
"?????????"
Returns: 7
"000000000000000000000100000000000010"
"?"
Returns: 2
"0000100000001000000010010001000000100000000"
"0"
Returns: 1
"000000000100010000010001000100"
"???????????????"
Returns: 16
"00001000"
"??????"
Returns: 3
"000000010000000000000"
"?????"
Returns: 6
"000010000000000000001"
"?????????????"
Returns: 7
"011"
"?00"
Returns: 0
"1111111111111111111111111111111111111111"
"????????????????????????????????????????"
Returns: 1
"10"
"01"
Returns: 0
"0"
"????"
Returns: 0
"10101"
"1????"
Returns: 1
"0101010101010111100101010"
"??"
Returns: 4
"100"
"?1?"
Returns: 0
"1111"
"0000"
Returns: 0
"0001"
"01"
Returns: 1
"0"
"0"
Returns: 1
"000"
"11?"
Returns: 0
"11111"
"11"
Returns: 1
"10101010101010101010101010101010101010101010101010"
"??????????????????????????????????????????????????"
Returns: 1
"11111111111111111111111111111111111111111111111"
"???????????????????????????????????????????11"
Returns: 1
"01"
"01"
Returns: 1
"00010001"
"10"
Returns: 1
"01"
"?1"
Returns: 1
"00"
"?"
Returns: 1
"011110"
"10"
Returns: 1
"11"
"01"
Returns: 0
"00"
"11"
Returns: 0
"0000000"
"??"
Returns: 1