Statistics

Problem Statement for "NonDeterministicSubstring"

Problem Statement

You are given two Strings: A and B. Each character in A is either '0' or '1'. Each character in B is '0', '1', or '?'.


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

  1. "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.

  2. "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.

  3. "001010101100010111010"

    "???"

    Returns: 8

    Each of the 8 strings that match B occurs somewhere in A.

  4. "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.

  5. "1101010101111010101011111111110001010101"

    "???????????????????????????????????"

    Returns: 6

  6. "1100011000010000001011000010101110110"

    "?011101??000?0"

    Returns: 0

  7. "0010111100001001111111010101001000"

    "1?100???00??1???000"

    Returns: 0

  8. "01001001111011101100010010111111001111111110101110"

    "10?1111"

    Returns: 2

  9. "1110110111001001110001001101000011011111101100001"

    "0"

    Returns: 1

  10. "101101101010100101100100011111001100111010110"

    "?10100?1?0110?00100?1???01??1100"

    Returns: 0

  11. "00100011100011000110111010011011011000001"

    "11111?110010?001?1100?01001111110110"

    Returns: 0

  12. "0100000010111001010101"

    "0110?1000"

    Returns: 0

  13. "11001111000001"

    "10001011?10?"

    Returns: 0

  14. "1000001011111000000111101001101001111"

    "010101?"

    Returns: 0

  15. "1111011111001010001111101000100111011110101011010"

    "10111?11?110????010?0110??01100??0?1?0?1000000?"

    Returns: 0

  16. "11111110011100000110000101111010001110100110"

    "0???01???????"

    Returns: 3

  17. "1111011000"

    "0?1?????0"

    Returns: 0

  18. "1010011100001000101101010101"

    "????????????????0??????"

    Returns: 2

  19. "11100000110110100000111100111100011110011101010101"

    "????0?????10????????0"

    Returns: 1

  20. "111101011101010110000000110101100111110"

    "10??1?0??1???1??"

    Returns: 0

  21. "111100001111000110001"

    "?????????0????1??"

    Returns: 0

  22. "11010000111100001011100111101011000001111100010001"

    "????"

    Returns: 16

  23. "01110000110001110001101111000011"

    "???????????0?0??1?0"

    Returns: 0

  24. "1110011111100010011101101100010000101111110"

    "1???????0?0??????0?????????1???????0??"

    Returns: 0

  25. "11000000001101100010000101000100011111000010000"

    "???10????1"

    Returns: 2

  26. "1011111111110111101000001110101011100000010"

    "??????1??"

    Returns: 18

  27. "001111010111011011100000101111111110"

    "1?????1??????????????0??00?????????"

    Returns: 0

  28. "00111011111110110011010101110111001011110001101110"

    "????1?????0"

    Returns: 12

  29. "110011011001011111110100011101100"

    "?00?????0????1??1???????1???1?0??"

    Returns: 0

  30. "0010010011000001000000011001111000111000111"

    "??0??1????00?"

    Returns: 1

  31. "000010011110111000"

    "??0?1?????"

    Returns: 3

  32. "110101111101010000010100111110111010100"

    "????????1????11???????????0????"

    Returns: 0

  33. "110101111110000001010100101011111"

    "????0??????????????1??????"

    Returns: 2

  34. "011100101001010110010001011000011101"

    "???0?1?????????????????????????????"

    Returns: 1

  35. "0011100011011000101010000101011010"

    "0????????????????1?????"

    Returns: 2

  36. "00000000100"

    "?????????"

    Returns: 3

  37. "0011001000100"

    "????????"

    Returns: 6

  38. "000000000100001000000001000011000000000010000"

    "??????0?????????????????????"

    Returns: 15

  39. "0100010000000000"

    "?????????"

    Returns: 7

  40. "000000000000000000000100000000000010"

    "?"

    Returns: 2

  41. "0000100000001000000010010001000000100000000"

    "0"

    Returns: 1

  42. "000000000100010000010001000100"

    "???????????????"

    Returns: 16

  43. "00001000"

    "??????"

    Returns: 3

  44. "000000010000000000000"

    "?????"

    Returns: 6

  45. "000010000000000000001"

    "?????????????"

    Returns: 7

  46. "011"

    "?00"

    Returns: 0

  47. "1111111111111111111111111111111111111111"

    "????????????????????????????????????????"

    Returns: 1

  48. "10"

    "01"

    Returns: 0

  49. "0"

    "????"

    Returns: 0

  50. "10101"

    "1????"

    Returns: 1

  51. "0101010101010111100101010"

    "??"

    Returns: 4

  52. "100"

    "?1?"

    Returns: 0

  53. "1111"

    "0000"

    Returns: 0

  54. "0001"

    "01"

    Returns: 1

  55. "0"

    "0"

    Returns: 1

  56. "000"

    "11?"

    Returns: 0

  57. "11111"

    "11"

    Returns: 1

  58. "10101010101010101010101010101010101010101010101010"

    "??????????????????????????????????????????????????"

    Returns: 1

  59. "11111111111111111111111111111111111111111111111"

    "???????????????????????????????????????????11"

    Returns: 1

  60. "01"

    "01"

    Returns: 1

  61. "00010001"

    "10"

    Returns: 1

  62. "01"

    "?1"

    Returns: 1

  63. "00"

    "?"

    Returns: 1

  64. "011110"

    "10"

    Returns: 1

  65. "11"

    "01"

    Returns: 0

  66. "00"

    "11"

    Returns: 0

  67. "0000000"

    "??"

    Returns: 1


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: