Problem Statement
Consider a string consisting of lowercase characters and following two operations that can change it:
- Next: Choose a lowercase character other than 'z' and replace its every occurrence with the next character ('a' -> 'b', 'b' -> 'c', ..., 'x' -> 'y', 'y' -> 'z').
- Prev: Choose a lowercase character other than 'a' and replace its every occurrence with the previous character ('b' -> 'a', 'c' -> 'b', ..., 'y' -> 'x', 'z' -> 'y').
Definition
- Class:
- NextOrPrev
- Method:
- getMinimum
- Parameters:
- int, int, String, String
- Returns:
- int
- Method signature:
- int getMinimum(int nextCost, int prevCost, String start, String goal)
- (be sure your method is public)
Constraints
- nextCost and prevCost will each be between 1 and 1000, inclusive.
- start and goal will each contain between 1 and 26 characters, inclusive.
- start and goal will contain the same number of characters.
- Each character in start and goal will be a lowercase character.
- The characters in start will be distinct.
- The characters in goal will be distinct.
Examples
5
8
"ae"
"bc"
Returns: 21
There are several optimal sequences of operations. Here is one of them: "ae" -(Next)-> "be" -(Prev)-> "bd" -(Prev)-> "bc". The total cost is 5 + 8 + 8 = 21.5
8
"ae"
"cb"
Returns: -1
It is impossible to transform "ae" into "cb".1
1
"srm"
"srm"
Returns: 0
start and goal may be the same. The cost of an empty sequence of operations is 0.10
1
"acb"
"bdc"
Returns: 30
10
1
"zyxw"
"vuts"
Returns: 16
563
440
"ptrbgcnlaizo"
"rtscedkiahul"
Returns: 10295
126
311
"yovlkwpjgsna"
"zpwnkytjisob"
Returns: -1