TopCoder problem "EqualizeStrings" used in TCO10 Round 1 (Division I Level One)



Problem Statement

    

You have two Strings, s and t, and you want them to be equal. You can change individual letters in the Strings, but you cannot add or remove letters. In a single move, you can change any one letter in one String to the letter that comes directly before or after it in the alphabet. The alphabet wraps around, so you can also change 'a' to 'z' or 'z' to 'a'. You want to make the two Strings equal using the minimum possible number of moves. Return the resulting String. If there are multiple answers, return the one that comes earliest alphabetically.

 

Definition

    
Class:EqualizeStrings
Method:getEq
Parameters:String, String
Returns:String
Method signature:String getEq(String s, String t)
(be sure your method is public)
    
 

Notes

-A String comes earlier lexicographically than another one of the same length if and only if it has a character closer to the beginning of the alphabet in the first position at which they differ.
 

Constraints

-s will contain between 1 and 50 characters, inclusive.
-s and t will contain the same number of characters.
-Each character of s and t will be a lowercase letter ('a'-'z').
 

Examples

0)
    
"cat"
"dog"
Returns: "caa"
Use 1 move to change 'd' to 'c', 12 moves to change 'o' to 'a', 6 moves to change 'g' to 'a' and 7 moves to change 't' to 'a' for a total of 26 moves to get both Strings equal to "caa".
1)
    
"abcdefghijklmnopqrstuvwxyz"
"bcdefghijklmnopqrstuvwxyza"
Returns: "abcdefghijklmnopqrstuvwxya"
Change every letter in t to its previous letter in the alphabet, using exactly one move per letter, with the exception of the last character; it's preferable to change the 'z' in s to 'a' to obtain a lexicographically earlier solution.
2)
    
"programmingcompetitionsrule"
"programmingcompetitionsrule"
Returns: "programmingcompetitionsrule"
If both strings are equal, then you don't need any moves.
3)
    
"topcoderopen"
"onlinerounds"
Returns: "onlcndaoondn"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=14279&pm=10933

Writer:

soul-net

Testers:

PabloGilberto , ivan_metelsky , zhuzeyuan

Problem categories:

Greedy