Statistics

Problem Statement for "Drbalance"

Problem Statement

A plus/minus string is a string in which each character is either a '+' or a '-'.

The balance of a plus/minus string is computed as the number of '+' characters minus the number of '-' characters.

For example, the balance of the string "++-+" is 3-1 = 2, and the balance of the string "---" is 0-3 = -3.

The prefix of a string S is any string that can be obtained by removing some (possibly none, possibly all) characters from the end of S. For example, the prefixes of the string "++-+" are the strings "++-+", "++-", "++", "+", and "".

Given a plus/minus string, its negativity is the number of its prefixes that have a negative balance. For example, the negativity of the string "++-+" is 0, as none of its prefixes have a negative balance. The negativity of the string "---" is 3. Its three prefixes with a negative balance are "-", "--", and "---".

You are given a String s that is a plus/minus string. You are also given an int k. Your goal is to change s into a string with negativity at most k. In other words, you want to change s into a string that has at most k prefixes that have a negative balance.

In order to change s you are going to perform a sequence of zero or more steps. In each step you can change a single '-' character in s into a '+' or vice versa. Compute and return the smallest number of steps needed.

Definition

Class:
Drbalance
Method:
lesscng
Parameters:
String, int
Returns:
int
Method signature:
int lesscng(String s, int k)
(be sure your method is public)

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • k will be between 0 and the length of s, inclusive.
  • Each character in s will be either '+' or '-'.

Examples

  1. "---"

    1

    Returns: 1

    One step is sufficient. If we change character 0 of s into a '+', we will obtain the string "+--". This string has only one prefix with a negative balance - namely, the entire string "+--". As k=1, we have reached our goal.

  2. "+-+-"

    0

    Returns: 0

  3. "-+-+---"

    2

    Returns: 1

  4. "-------++"

    3

    Returns: 3

  5. "-+--+--+--++++----+"

    3

    Returns: 2

  6. "-----+-----+--++---+---++---+--+----+-+---"

    22

    Returns: 6

  7. "------------+--+-+-------+-+---+--"

    23

    Returns: 6

  8. "----+---------+--------+-++---++-++"

    21

    Returns: 6

  9. "+-----+---------+---+-+-----+-------+--"

    6

    Returns: 11

  10. "---++---+-"

    2

    Returns: 2

  11. "--+-++--+---+---+-+--------------+-----+-+++-"

    13

    Returns: 9

  12. "------++++++--+--+-++--+-+----+----++-"

    1

    Returns: 4

  13. "-++---+-++++------+-+---+++"

    8

    Returns: 2

  14. "-------++--+------------+++"

    13

    Returns: 4

  15. "--+++-+-++-----++---+----------++-+-----+-"

    8

    Returns: 6

  16. "+-------+-"

    1

    Returns: 3

  17. "-+--+--+----++--+++-+++--++-+---+--"

    3

    Returns: 3

  18. "-+-+---+--+--+--+--+++---"

    8

    Returns: 3

  19. "+--++---++--+-++-+-----+----+-+-----++"

    22

    Returns: 1

  20. "++---------+--+--------+----+-+-------"

    23

    Returns: 4

  21. "-+-------+--+----++------+--+-------+--+----"

    40

    Returns: 1

  22. "-------+---+--+-----+----+------+---"

    36

    Returns: 0

  23. "-+-++-+----++---+--------+-+--+"

    11

    Returns: 3

  24. "-++-----+---++++++---------+--+--+-++----"

    7

    Returns: 5

  25. "--+-+-+-+--+-+--++++++--------++-----+-----+---+-+"

    32

    Returns: 1

  26. "--+---+---++------+---"

    0

    Returns: 6

  27. "+------------+-----++------+-+-------+--+-------"

    15

    Returns: 11

  28. "---------++---+--"

    2

    Returns: 5

  29. "--+-----++-----+----+--------++--"

    10

    Returns: 7

  30. "------------------+-------+----+-+-+-----+--"

    29

    Returns: 8

  31. "-+-++-++---------+------+--------++-+-+++----+-+++"

    29

    Returns: 5

  32. "+---+--"

    4

    Returns: 1

  33. "-++++---+--+--+-+++----+-"

    2

    Returns: 1

  34. "-----+"

    3

    Returns: 2

  35. "+-------+------------+-----+-+++-----+-"

    1

    Returns: 12

  36. "----+--+-----++"

    12

    Returns: 2

  37. "---+------+-+-++--+--+---+-+------"

    28

    Returns: 2

  38. "------++--+---+---+--+---+----"

    25

    Returns: 2

  39. "----------++---+--------+--++-+-++-++--+--"

    17

    Returns: 8

  40. "+---+------++--+++-+-------++--+++-+----++"

    7

    Returns: 5

  41. "++-++---+-+------------+-------++--+--+-"

    12

    Returns: 7

  42. "+--+-+--+-+--++---+--+++---"

    7

    Returns: 2

  43. "-----+----------+--+---+-++---+++--"

    13

    Returns: 8

  44. "---++----+--++-----++--+--------+---+----"

    17

    Returns: 5

  45. "--+++-----+-----+-++----+-------+----+--"

    20

    Returns: 4

  46. "-+-+--++-++---++-+-++---"

    0

    Returns: 1

  47. "+---++--++++++-+-++++--+---++---+-+-"

    0

    Returns: 1

  48. "-----+-+-------+---+-++-+"

    6

    Returns: 6

  49. "-+-+-----++-+++-+---++++-++-+-+-"

    6

    Returns: 2

  50. "--++---+--+---+"

    11

    Returns: 1

  51. "-++-------+---++++----+++----++"

    4

    Returns: 4

  52. "-"

    1

    Returns: 0

  53. "+--+--+---+-+------+-+--+-----+----"

    9

    Returns: 5

  54. "+---++"

    1

    Returns: 1

  55. "---++-++----+---+-+---+---+--++--+"

    3

    Returns: 5

  56. "---+-+-++-++-+-+----+-+--+-++-+-+-+-+---+---+-++++"

    48

    Returns: 0

  57. "--+-+--+-+---+--+-++---+--------+-+-++++-+--"

    22

    Returns: 3

  58. "---++---++---+++------+----+----+-+--++--+------"

    3

    Returns: 9

  59. "+-+-+--+-+-+--++++-+-----++--+---"

    10

    Returns: 1

  60. "------"

    1

    Returns: 3

  61. "---+---+--++-+-+----+--+---------+--++-+-"

    30

    Returns: 3

  62. "++-+--+++-++---++-+-+++-------"

    2

    Returns: 0

  63. "+-+---+-----+---+++-------"

    3

    Returns: 5

  64. "------+-++-------+-----+-+---+--+----+---+----"

    41

    Returns: 2

  65. "++-----+++---++++-------+-+-+--+--++-++-++-"

    3

    Returns: 4

  66. "+++++++-+--+--+++--++---+----+"

    0

    Returns: 0

  67. "---------+-+----+----++-+-+--+---+"

    6

    Returns: 7

  68. "---+-----+-----+--------+--+-+-+---+++-+----++"

    1

    Returns: 11

  69. "+----+-+------+-++---+----++++----"

    11

    Returns: 5

  70. "+----++--------+---------++----++--+---+------"

    42

    Returns: 1

  71. "+-++---++--+----+-+-++--++---+-"

    11

    Returns: 2

  72. "----+--+--+-++-++------+-+++--+-"

    32

    Returns: 0

  73. "-+----+---+-----++---++++++-"

    3

    Returns: 5

  74. "---------+-+-+--++---+-+--+----------+-"

    33

    Returns: 3

  75. "---+-+--++-+----++--+------+--+-----++------+-"

    8

    Returns: 8

  76. "+++--"

    0

    Returns: 0

  77. "+----+-+----+++-+--++++-+-+--+---+-+--++"

    2

    Returns: 3

  78. "---+----+--+-----++-++--+-"

    7

    Returns: 5

  79. "-+---+----------++-++-+++-+---+"

    18

    Returns: 4

  80. "-++--+-----++-+-----++---++------+-"

    1

    Returns: 7

  81. "---+---------+----+--+-+-+--+-+-+--+-----++-+-+-+-"

    50

    Returns: 0

  82. "+---++----+-------++----+-+-+---+-+-----------+-+"

    37

    Returns: 2

  83. "---++--+---------+---+--+--+----++-+-+--+------"

    19

    Returns: 8

  84. "--++-+-+--+-----++-----+-+--+---+---+-++---"

    28

    Returns: 2

  85. "---------++------------+---+-----++---+-+----++-+"

    39

    Returns: 4

  86. "+-------++-+----+---++"

    14

    Returns: 2

  87. "+++---+-++++----+---+-------------+-------+------+"

    3

    Returns: 12

  88. "++++"

    0

    Returns: 0

  89. "---"

    0

    Returns: 2

  90. "--"

    0

    Returns: 1

  91. "-++++++"

    1

    Returns: 0

  92. "+--++-+--------+++--+"

    4

    Returns: 3

  93. "++--+++"

    3

    Returns: 0

  94. "--------------------------------------------------"

    0

    Returns: 25

  95. "-+-----"

    3

    Returns: 1

  96. "++++"

    2

    Returns: 0

  97. "+-+"

    3

    Returns: 0

  98. "-------------------+"

    1

    Returns: 9

  99. "-"

    0

    Returns: 1

  100. "+++++++"

    1

    Returns: 0

  101. "+-"

    0

    Returns: 0

  102. "-++"

    1

    Returns: 0

  103. "+++---"

    3

    Returns: 0

  104. "++++++++++++"

    3

    Returns: 0

  105. "++"

    1

    Returns: 0

  106. "+++++++++++"

    6

    Returns: 0

  107. "----------------+"

    1

    Returns: 8

  108. "-----"

    0

    Returns: 3

  109. "----"

    1

    Returns: 2

  110. "------------"

    0

    Returns: 6


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: