Problem Statement
- 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
1000
1000
"abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"
Returns: 0
1000
1000
"abcdefghijklm"
"nopqrstuvwxyz"
Returns: 169000
1000
1000
"nopqrstuvwxyz"
"abcdefghijklm"
Returns: 169000
150
328
"c"
"j"
Returns: 1050
189
105
"hj"
"fu"
Returns: 2289
676
594
"zq"
"bv"
Returns: -1
137
80
"np"
"va"
Returns: -1
217
14
"pzm"
"uya"
Returns: 1267
607
663
"ogw"
"pce"
Returns: -1
242
434
"kvq"
"lnp"
Returns: -1
879
940
"hetu"
"dcgo"
Returns: 23500
42
440
"oria"
"uwst"
Returns: -1
417
149
"djvkh"
"akysg"
Returns: 5600
976
588
"ypubd"
"tqpai"
Returns: -1
494
615
"gimjhx"
"afqkbt"
Returns: 14155
210
438
"bipgzoy"
"alsbzpt"
Returns: 6288
586
935
"ilwjdpfn"
"emxgaubs"
Returns: 20122
359
525
"sueqyxthl"
"orbntspdf"
Returns: 19425
273
578
"ufagnqxeso"
"tfbjmrxesp"
Returns: 2794
156
106
"knpucjavzef"
"qruvhpeyzln"
Returns: 7644
582
48
"sadebkfnotx"
"tbhigqnsuvx"
Returns: -1
563
440
"ptrbgcnlaizo"
"rtscedkiahul"
Returns: 10295
126
311
"yovlkwpjgsna"
"zpwnkytjisob"
Returns: -1
920
627
"ikxunovswelrt"
"ghyvknwrxbjos"
Returns: 15593
193
494
"mxltredcabswv"
"ozmsqlkhbirut"
Returns: -1
884
512
"nufaseixrzbqh"
"deifukhtxcanb"
Returns: -1
235
986
"ykesljtouvbfic"
"ypjtrouswxbmni"
Returns: 11515
907
213
"tlqxpyswvfhmkb"
"rknymzpwvbflhc"
Returns: -1
520
88
"nslqpfvwmkrcgb"
"kxgcrhznyovwep"
Returns: -1
398
548
"noaixgtydurkcvq"
"lmbhxgszetpjdun"
Returns: 9264
768
666
"faugpikqnodxvbzy"
"gbshoijrmnevudzw"
Returns: 12702
27
314
"rxswhmdiyvfeoatpb"
"qwrvhjdiztfekbsnc"
Returns: 5105
743
756
"lvnbxeqpodizwshkcf"
"kulawdpnmcgzvqfhbe"
Returns: 18900
123
367
"cuqypviwjterxzbogdn"
"ernxmsivjqgpwyblhfk"
Returns: 10770
731
657
"vwmgosyetihqkrlapxfd"
"wxniotzeukjqlrmbpyhc"
Returns: 13815
42
267
"lcxpvsuhoqejimfytkgzb"
"lbwpvruhoqdjinfyskgza"
Returns: 1644
886
199
"jyvlqpwbifagxsuzkmtcn"
"jxumqovdhfcgwrtyknsep"
Returns: -1
279
449
"sfugtrekbzaxypwijlhmcn"
"retfsqdkbzavwouhjlgmcn"
Returns: 7184
80
760
"nywurdavxcfthkozbegilm"
"nywspdatxcgqjkozbehilm"
Returns: -1
652
855
"umrzwesxjotcpqgkiynfla"
"tvqkicabjesmopwlfxhdrz"
Returns: -1
36
729
"xfjkimhprtbcnudzywegsvq"
"xfkljniprtacoudzywegsvq"
Returns: 945
96
626
"zdohrxqgklvutpsmayniefw"
"zbohrxpgjkvutqslaynicfw"
Returns: -1
816
305
"pwolgbicnytfaumqzjrsvekd"
"qxpmibjcoyugavnrzkstweld"
Returns: 14688
177
675
"altzrmbidjnkhevqofwsucxp"
"aktzrlbheinjgdwqofxsucyp"
Returns: -1
607
12
"mgsdalrxqypiuvtzfocbkhnj"
"uwxeqrpmcdslkbhayzgfjvot"
Returns: -1
87
958
"kmrjqsatfudhxognypcvbeiwl"
"kmrjqtaufvdhyognzpcwbeixl"
Returns: 609
106
886
"qxmfedgojlurcwptyniakshvb"
"qxmfedgojlvrcyptzniakshwb"
Returns: -1
845
299
"pavknqfozxmydglehirtjcuwsb"
"pavknqfozxmydglehirtjcuwsb"
Returns: 0
899
293
"iqyopksawrhlxtceumdfgvnzbj"
"iqyopksawrhlxtbeumdfgvnzcj"
Returns: -1
696
39
"qfhobpxvjntureigkzalscmdwy"
"qmhxbpuljgkoreintvazscfdwy"
Returns: -1
12
56
"wvu"
"xyz"
Returns: -1
1
1
"ab"
"ba"
Returns: -1
10
10
"hm"
"mh"
Returns: -1
1
2
"abc"
"xyz"
Returns: 69
4
2
"abc"
"abc"
Returns: 0
1
1
"ab"
"de"
Returns: 6
1000
1
"aeiou"
"bcjzv"
Returns: -1
1
1
"ab"
"ac"
Returns: 1
999
998
"abcdefghijklmnopqrstuvwxyz"
"bcdefghijklmnopqrstuvwxyza"
Returns: -1
10
10
"ac"
"eg"
Returns: 80
5
10
"bc"
"da"
Returns: -1
5
8
"ab"
"dc"
Returns: -1
1000
1000
"zxcvbnmas"
"xcvbnmasr"
Returns: -1
1
1
"ah"
"ha"
Returns: -1
1000
1
"ab"
"yz"
Returns: 48000
1000
1000
"zywx"
"vust"
Returns: 16000
1
1
"ab"
"dc"
Returns: -1
1
1
"ba"
"cd"
Returns: -1
3
4
"ab"
"dc"
Returns: -1
2
2
"ac"
"cd"
Returns: 6
7
15
"ab"
"dc"
Returns: -1
123
123
"ab"
"cf"
Returns: 738
1
1
"sq"
"qs"
Returns: -1
10
100
"db"
"ac"
Returns: -1
5
8
"abc"
"def"
Returns: 45
10
12
"ac"
"ef"
Returns: 70
10
5
"ab"
"dc"
Returns: -1
2
2
"ba"
"ca"
Returns: 2
1
1
"zb"
"ya"
Returns: 2
1000
1000
"abcdefghijklz"
"mnopqrstuvwxy"
Returns: 145000
5
10
"ab"
"zc"
Returns: -1
2
3
"ab"
"zy"
Returns: -1
100
250
"cdef"
"bacd"
Returns: -1
1
1
"d"
"e"
Returns: 1
1
1
"zy"
"wx"
Returns: -1
5
10
"dc"
"ac"
Returns: -1
1
999
"b"
"a"
Returns: 999
1
1
"ac"
"zb"
Returns: -1
25
15
"ac"
"de"
Returns: 125