TopCoder problem "NumberChanger" used in SRM 221 (Division I Level Two , Division II Level Three)



Problem Statement

    You will be given 2 numbers start and finish each with the same number of digits. Both may have leading zeros. You are going to transform start into finish using the following kinds of transformation steps:
  • 1) Increment a digit less than 9 by 1. For example, changing 354 to 364.
  • 2) Decrement a digit greater than 0 by 1. For example, changing 354 to 254.
  • 3) Swapping two digits. For example, changing 354 to 453.
Return the fewest number of transformation steps required to change start into finish.
 

Definition

    
Class:NumberChanger
Method:transform
Parameters:String, String
Returns:int
Method signature:int transform(String start, String finish)
(be sure your method is public)
    
 

Constraints

-start will contain between 1 and 8 characters inclusive.
-Each character in start will be a digit ('0'-'9').
-finish will contain the same number of characters as start.
 

Examples

0)
    
"01234567"
"01234567"
Returns: 0
No steps required.
1)
    
"11119999"
"99991111"
Returns: 4
4 swaps does the trick.
2)
    
"55555555"
"12345678"
Returns: 16
Swapping is of no use here.
3)
    
"77771111"
"00446688"
Returns: 16
4)
    
"12"
"30"
Returns: 3

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=2970

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5867&pm=2970

Writer:

AdminBrett

Testers:

PabloGilberto , lbackstrom

Problem categories:

Brute Force