Problem Statement
Andrew has a combination lock. The lock consists of multiple dials that are placed next to each other. Each dial contains the digits 0 through 9, in order. At any moment, exactly one of the digits on each dial is visible. The string formed by the currently visible digits is called the current combination.
The visible digit on a dial can be changed by rotating the dial up or down. Rotating the dial up changes 0 to 1, 1 to 2, and so on. Note that the digits on a dial wrap around: if we rotate up a dial that shows a 9, it will show a 0 again. Naturally, rotating the dial down changes the digit in the other direction.
We are able to rotate multiple dials at the same time, as long as they are next to each other. More precisely, in a single turn we can take an arbitrarily long segment of consecutive dials, and rotate all of them one step in the same direction (i.e., either all of them up, or all of them down).
For example, suppose that the current combination is "123". In one step, we can change it to many different combinations, including "012" (all three dials down), "234" (all three dials up), "133" (middle dial up), and "013" (first two dials down). Note that we cannot change "123" to "224" in a single step.
You are given two
Definition
- Class:
- CombinationLockDiv2
- Method:
- minimumMoves
- Parameters:
- String, String
- Returns:
- int
- Method signature:
- int minimumMoves(String S, String T)
- (be sure your method is public)
Constraints
- S will contain between 1 and 50 characters, inclusive.
- S will contain the same number of characters as T.
- Each character in S and T will be a digit ('0'-'9').
Examples
"123"
"112"
Returns: 1
Rotate the last two dials down.
"1"
"7"
Returns: 4
Rotate the dial down 4 times: from 1 to 0, from 0 to 9, from 9 to 8, and from 8 to 7.
"607"
"607"
Returns: 0
"1234"
"4567"
Returns: 3
"020"
"909"
Returns: 2
"4423232218340"
"6290421476245"
Returns: 18
"0183785"
"0000000"
Returns: 11
"33333222322233333"
"00000000000000000"
Returns: 5
"0123456789876543210"
"0000000000000000000"
Returns: 9
"1234567890987654321"
"0000000000000000000"
Returns: 10
"3789656"
"5159478"
Returns: 10
"9277824328"
"5457494629"
Returns: 16
"351832397"
"783933521"
Returns: 8
"355070204"
"676060327"
Returns: 7
"7426453582937390990599"
"5243281321698807518024"
Returns: 36
"176734746409328221083"
"164499178500552788873"
Returns: 10
"574149036467923059067"
"340682246346935394635"
Returns: 12
"605671109043778650486220152344964"
"052157799605842052820321751543033"
Returns: 36
"996315769104193210743926563394033848332"
"873969080192858642844150020184156294011"
Returns: 20
"690221017521430132366153071208355064400"
"466764227408084453356276427987367309078"
Returns: 21
"22109241798036080249208643709135880975390863"
"80152752534914222910829807569979861784561356"
Returns: 64
"91764076679014302854836840311218635202200369261782"
"10590882164643449864431120142394117791098627375510"
Returns: 70
"3893718543803167289010912803640497820625097763959"
"7114706208235266055553122926096176832960665664183"
Returns: 28
"4199201645518106482753617475793802816510517321777"
"6298077188728083036074605376927369606633963000789"
Returns: 26
"42323221834062904"
"21476245378965651"
Returns: 21
"94789277824328545749462"
"97839335216760603277426"
Returns: 34
"453582937390990599524328132169880751802416449917"
"850055278887334068224634693539463560567110904377"
Returns: 58
"650486220152344964052157799605842052"
"820321751543033873969080192858642844"
Returns: 41
"500201"
"841562"
Returns: 9
"4011466764227408084453356276427987367309"
"0782210924179803608024920864370913588097"
Returns: 52
"5390863801527525349142229108298075699798617845613"
"5691764076679014302854836840311218635202200369261"
Returns: 61
"82105908821646434498644311201423"
"94117791098627375510711470620823"
Returns: 38
"26605555312292609617683"
"29606656641836298077188"
Returns: 28
"72808303607460537692736960663396300078966827177"
"97188800723106837706034782220291427370931711916"
Returns: 62
"2390069375279223989186826963447017"
"7324073849890926537064120570111519"
Returns: 45
"2979790780958516619352192169474637319864014737006"
"8179431995047028504606260522563487301520334596840"
Returns: 63
"3"
"4"
Returns: 1
"83216"
"67458"
Returns: 8
"16436173113153215"
"63398511567666159"
Returns: 24
"16903571944946235956386790292932226"
"82446781821590556968621368193166783"
Returns: 20
"25934843597600221049999827170062208"
"91477053474254542051234495071296765"
Returns: 20
"44980990286683930856871328173960974"
"10423100163237251868116996074194431"
Returns: 20
"030493936306021114157883801666813551900"
"917047257394786546258017368456936907689"
Returns: 20
"112095956"
"544196180"
Returns: 8
"123456789"
"000000000"
Returns: 9
"01234567890123456789"
"00000000000000000000"
Returns: 18
"132"
"000"
Returns: 3
"555555546555"
"000000000000"
Returns: 7
"1233321"
"0000000"
Returns: 3
"91919191"
"00000000"
Returns: 8
"919191919"
"000000000"
Returns: 9
"1213141516171819"
"0000000000000000"
Returns: 24
"12123234345456567678789890901012123234345456567678"
"00000000000000000000000000000000000000000000000000"
Returns: 33
"121232343454565676787898909010"
"000000000000000000000000000000"
Returns: 19
"01234567890123456789012345678901234567890123456789"
"00000000000000000000000000000000000000000000000000"
Returns: 45
"07418529630741852963074185296307418529630741852963"
"00000000000000000000000000000000000000000000000000"
Returns: 105
"00236123469719800"
"00000000000000000"
Returns: 16
"1"
"0"
Returns: 1
"9"
"0"
Returns: 1
"00000000000000000000000000000000000000000000000000"
"50505050505050505050505050505050505050505050505050"
Returns: 125
about max answer
"12347987984732789432009090909090904218989432814923"
"21447812739923746784821749837498209099098423788737"
Returns: 63
"12345678901234567890123456789012345678901234567890"
"22222222222222222222222222222222222222222222222222"
Returns: 45
"44232322184423232218442323221844232322184423232218"
"90421476245629042147624562904214762456290421476245"
Returns: 68
"11111111"
"33344444"
Returns: 3
"01234567890123456789012345678901234567890123456789"
"01234567896290420123456789147012345678962012345645"
Returns: 24
"3094850397852098742039840329840239840329840329840"
"2093802398403274983247320987402398402759328649328"
Returns: 57
"1234567891234567891234567891234567891234567897648"
"5476541654324532416543213245215446564554324132416"
Returns: 54
"045515484151269715154154697784178841520215461"
"788415202154610215454113645548704551548415124"
Returns: 52
"123"
"135"
Returns: 2
"61245315859136861998168131400331549386718533622839"
"34852109789707835297031580091362740674655155564356"
Returns: 67
"90834571634876398763898762123214789098764859860987"
"09847564987456321456098374623432345687654758909874"
Returns: 46
"36753562912709360626187920237592289736129319478450"
"00000000000000000000000000000000000000000000000000"
Returns: 69
"0550"
"5005"
Returns: 5
"119088"
"880911"
Returns: 8
"11223344556677889"
"99323253555786869"
Returns: 11
"11111"
"23232"
Returns: 3
"87895715891234567890123456789087219851691234567890"
"49386571094961830527496183052784759817584961830527"
Returns: 85
"87589123761512387652875891237615123876528758912376"
"42167128923471982741892374918278149471231212321348"
Returns: 67
"123456789"
"987654321"
Returns: 16
"12221212156546121336664666464566465400000001115511"
"11221313465464640012215454100008448779451512121212"
Returns: 50
"590"
"681"
Returns: 3
"5555559555555"
"0000007000000"
Returns: 8
"472547254725472547254725472547254725472547254725"
"000000000000000000000000000000000000000000000000"
Returns: 76
"0168511300"
"0947977456"
Returns: 12