Statistics

Problem Statement for "StrangeComputer"

Problem Statement

You have been given a very strange computer. Its memory consists of several bits, each initially set to 0, and it can only perform the following type of operation:
Choose one of the bits in memory, and choose a value - 0 or 1. All the bits between the selected bit and the last bit in memory, inclusive, will be set to the chosen value. For example, if the memory is "0010", and you choose the second bit and a value of 1, the memory will change to "0111".

You are given a String mem. The number of characters in mem is equal to the number of bits in the computer's memory. Return the minimum number of operations required to set the computer's memory equal to mem.

Definition

Class:
StrangeComputer
Method:
setMemory
Parameters:
String
Returns:
int
Method signature:
int setMemory(String mem)
(be sure your method is public)

Constraints

  • mem will contain between 1 and 50 characters, inclusive.
  • mem will contain only the characters '0' (zero) or '1' (one).

Examples

  1. "0011"

    Returns: 1

    Choose the third bit and a value of 1.

  2. "000"

    Returns: 0

    No operations are needed.

  3. "0100"

    Returns: 2

    0000 -> 0111 -> 0100

  4. "111000111"

    Returns: 3

  5. "0"

    Returns: 0

  6. "1"

    Returns: 1

  7. "00"

    Returns: 0

  8. "11"

    Returns: 1

  9. "01"

    Returns: 1

  10. "10"

    Returns: 2

  11. "00000000000000000000000000000000000000000000000000"

    Returns: 0

  12. "10000000000000000000000000000000000000000000000000"

    Returns: 2

  13. "00000000000000000000000000000000000000000000000001"

    Returns: 1

  14. "10101010101010101010101010101010101010101010101010"

    Returns: 50

  15. "01010101010101010101010101010101010101010101010101"

    Returns: 49

  16. "01010101010101010101010101010101010101010101010111"

    Returns: 47

  17. "11111111111111111111111111111111111111111111111111"

    Returns: 1

  18. "11111111111111111111111111111111111111111111111110"

    Returns: 2

  19. "01111111111111111111111111111111111111111111111111"

    Returns: 1

  20. "01111001101011000001011000111100011101011110100101"

    Returns: 25

  21. "10010001110101011101010100101000001101000010000110"

    Returns: 30

  22. "01110100010011101011111111110110101100101110001010"

    Returns: 26

  23. "0101111000101000001011111001101101011111001101001"

    Returns: 25

  24. "100000001110010101010011001000111001001100001011"

    Returns: 25

  25. "001000001000001000001100111101001111011100100"

    Returns: 18

  26. "00000011010100011000101101111101101101000000101"

    Returns: 23

  27. "00000110100010000111001001110010010111101100100000"

    Returns: 22

  28. "000100000010111000100100011011111001001011"

    Returns: 19

  29. "00100010000011110010110111010110110111110011110011"

    Returns: 23

  30. "1000110010011111010110000100100000000001001"

    Returns: 19

  31. "101111000100010110110110000001001111101110111110"

    Returns: 22

  32. "01101001001111001111100111000011111011111100001100"

    Returns: 18

  33. "11000001000111000111101110000111011011"

    Returns: 15

  34. "01010100010100101100111010100010111111011001011000"

    Returns: 30

  35. "11111101100110111010110110010010001100"

    Returns: 20

  36. "100100011001010100100010000100110100001111110"

    Returns: 24

  37. "01111110010000010010111000011001110010111"

    Returns: 17

  38. "10011010001110111010111010001100101101"

    Returns: 23

  39. "0110011100101001101001000001011000011001010110110"

    Returns: 28

  40. "1011101111001000110010111110011"

    Returns: 15

  41. "0001010000010110010111010110101111010010000"

    Returns: 24

  42. "11011010011011101010010011111"

    Returns: 17

  43. "10000101000111001000001010001"

    Returns: 15

  44. "11011011110101111011110001101010111010001000"

    Returns: 24

  45. "1101010111100110110010010010011101101110011"

    Returns: 25

  46. "00110101100001010000110010111"

    Returns: 15

  47. "01011001111011100001010101111011"

    Returns: 17

  48. "10000111011011011110000111010110"

    Returns: 16

  49. "101111110101010100111100101111001"

    Returns: 19

  50. "1011110100000101010"

    Returns: 12

  51. "000111010111100111"

    Returns: 7

  52. "1111100101101110000"

    Returns: 8

  53. "01101000110111001011010101011010"

    Returns: 22

  54. "001001001111110010000"

    Returns: 8

  55. "011100000101110111000"

    Returns: 8

  56. "11001001110100111"

    Returns: 9

  57. "01010011111011111010111"

    Returns: 11

  58. "01110000000001"

    Returns: 3

  59. "1011000100101010000110110111"

    Returns: 17

  60. "0101001100111101"

    Returns: 9

  61. "1101011110001110000011"

    Returns: 9

  62. "1011100110000110010"

    Returns: 10

  63. "000000101000101000010"

    Returns: 10

  64. "1010000110111110100"

    Returns: 10

  65. "01100100000101011000011"

    Returns: 11

  66. "0011000110111110111111"

    Returns: 7

  67. "0101010111101110"

    Returns: 10

  68. "110001011010110"

    Returns: 10

  69. "01110011110001110"

    Returns: 6

  70. "1010101001010101010101010100111111"

    Returns: 27

  71. "10101010"

    Returns: 8

  72. "111"

    Returns: 1

  73. "0001"

    Returns: 1

  74. "0000"

    Returns: 0


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: