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
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
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.
"+-+-"
0
Returns: 0
"-+-+---"
2
Returns: 1
"-------++"
3
Returns: 3
"-+--+--+--++++----+"
3
Returns: 2
"-----+-----+--++---+---++---+--+----+-+---"
22
Returns: 6
"------------+--+-+-------+-+---+--"
23
Returns: 6
"----+---------+--------+-++---++-++"
21
Returns: 6
"+-----+---------+---+-+-----+-------+--"
6
Returns: 11
"---++---+-"
2
Returns: 2
"--+-++--+---+---+-+--------------+-----+-+++-"
13
Returns: 9
"------++++++--+--+-++--+-+----+----++-"
1
Returns: 4
"-++---+-++++------+-+---+++"
8
Returns: 2
"-------++--+------------+++"
13
Returns: 4
"--+++-+-++-----++---+----------++-+-----+-"
8
Returns: 6
"+-------+-"
1
Returns: 3
"-+--+--+----++--+++-+++--++-+---+--"
3
Returns: 3
"-+-+---+--+--+--+--+++---"
8
Returns: 3
"+--++---++--+-++-+-----+----+-+-----++"
22
Returns: 1
"++---------+--+--------+----+-+-------"
23
Returns: 4
"-+-------+--+----++------+--+-------+--+----"
40
Returns: 1
"-------+---+--+-----+----+------+---"
36
Returns: 0
"-+-++-+----++---+--------+-+--+"
11
Returns: 3
"-++-----+---++++++---------+--+--+-++----"
7
Returns: 5
"--+-+-+-+--+-+--++++++--------++-----+-----+---+-+"
32
Returns: 1
"--+---+---++------+---"
0
Returns: 6
"+------------+-----++------+-+-------+--+-------"
15
Returns: 11
"---------++---+--"
2
Returns: 5
"--+-----++-----+----+--------++--"
10
Returns: 7
"------------------+-------+----+-+-+-----+--"
29
Returns: 8
"-+-++-++---------+------+--------++-+-+++----+-+++"
29
Returns: 5
"+---+--"
4
Returns: 1
"-++++---+--+--+-+++----+-"
2
Returns: 1
"-----+"
3
Returns: 2
"+-------+------------+-----+-+++-----+-"
1
Returns: 12
"----+--+-----++"
12
Returns: 2
"---+------+-+-++--+--+---+-+------"
28
Returns: 2
"------++--+---+---+--+---+----"
25
Returns: 2
"----------++---+--------+--++-+-++-++--+--"
17
Returns: 8
"+---+------++--+++-+-------++--+++-+----++"
7
Returns: 5
"++-++---+-+------------+-------++--+--+-"
12
Returns: 7
"+--+-+--+-+--++---+--+++---"
7
Returns: 2
"-----+----------+--+---+-++---+++--"
13
Returns: 8
"---++----+--++-----++--+--------+---+----"
17
Returns: 5
"--+++-----+-----+-++----+-------+----+--"
20
Returns: 4
"-+-+--++-++---++-+-++---"
0
Returns: 1
"+---++--++++++-+-++++--+---++---+-+-"
0
Returns: 1
"-----+-+-------+---+-++-+"
6
Returns: 6
"-+-+-----++-+++-+---++++-++-+-+-"
6
Returns: 2
"--++---+--+---+"
11
Returns: 1
"-++-------+---++++----+++----++"
4
Returns: 4
"-"
1
Returns: 0
"+--+--+---+-+------+-+--+-----+----"
9
Returns: 5
"+---++"
1
Returns: 1
"---++-++----+---+-+---+---+--++--+"
3
Returns: 5
"---+-+-++-++-+-+----+-+--+-++-+-+-+-+---+---+-++++"
48
Returns: 0
"--+-+--+-+---+--+-++---+--------+-+-++++-+--"
22
Returns: 3
"---++---++---+++------+----+----+-+--++--+------"
3
Returns: 9
"+-+-+--+-+-+--++++-+-----++--+---"
10
Returns: 1
"------"
1
Returns: 3
"---+---+--++-+-+----+--+---------+--++-+-"
30
Returns: 3
"++-+--+++-++---++-+-+++-------"
2
Returns: 0
"+-+---+-----+---+++-------"
3
Returns: 5
"------+-++-------+-----+-+---+--+----+---+----"
41
Returns: 2
"++-----+++---++++-------+-+-+--+--++-++-++-"
3
Returns: 4
"+++++++-+--+--+++--++---+----+"
0
Returns: 0
"---------+-+----+----++-+-+--+---+"
6
Returns: 7
"---+-----+-----+--------+--+-+-+---+++-+----++"
1
Returns: 11
"+----+-+------+-++---+----++++----"
11
Returns: 5
"+----++--------+---------++----++--+---+------"
42
Returns: 1
"+-++---++--+----+-+-++--++---+-"
11
Returns: 2
"----+--+--+-++-++------+-+++--+-"
32
Returns: 0
"-+----+---+-----++---++++++-"
3
Returns: 5
"---------+-+-+--++---+-+--+----------+-"
33
Returns: 3
"---+-+--++-+----++--+------+--+-----++------+-"
8
Returns: 8
"+++--"
0
Returns: 0
"+----+-+----+++-+--++++-+-+--+---+-+--++"
2
Returns: 3
"---+----+--+-----++-++--+-"
7
Returns: 5
"-+---+----------++-++-+++-+---+"
18
Returns: 4
"-++--+-----++-+-----++---++------+-"
1
Returns: 7
"---+---------+----+--+-+-+--+-+-+--+-----++-+-+-+-"
50
Returns: 0
"+---++----+-------++----+-+-+---+-+-----------+-+"
37
Returns: 2
"---++--+---------+---+--+--+----++-+-+--+------"
19
Returns: 8
"--++-+-+--+-----++-----+-+--+---+---+-++---"
28
Returns: 2
"---------++------------+---+-----++---+-+----++-+"
39
Returns: 4
"+-------++-+----+---++"
14
Returns: 2
"+++---+-++++----+---+-------------+-------+------+"
3
Returns: 12
"++++"
0
Returns: 0
"---"
0
Returns: 2
"--"
0
Returns: 1
"-++++++"
1
Returns: 0
"+--++-+--------+++--+"
4
Returns: 3
"++--+++"
3
Returns: 0
"--------------------------------------------------"
0
Returns: 25
"-+-----"
3
Returns: 1
"++++"
2
Returns: 0
"+-+"
3
Returns: 0
"-------------------+"
1
Returns: 9
"-"
0
Returns: 1
"+++++++"
1
Returns: 0
"+-"
0
Returns: 0
"-++"
1
Returns: 0
"+++---"
3
Returns: 0
"++++++++++++"
3
Returns: 0
"++"
1
Returns: 0
"+++++++++++"
6
Returns: 0
"----------------+"
1
Returns: 8
"-----"
0
Returns: 3
"----"
1
Returns: 2
"------------"
0
Returns: 6