TopcoderArchiveVisit Topcoder
SRM22 Nov 2011
SRM 534

EllysString

Problem statement, definition, constraints, and public examples.

Problem Statement

Elly has a string S. She wants to convert it to her favorite string T. You are given two String[]s s and t. Concatenate all elements of s to get the string S. Concatenate all elements of t to get the string T. S and T will contain the same number of characters.

Elly can perform two types of operations:
  • Choose a character and replace it with another character. For example, she can change "topcoder" to "topcodfr" by replacing character 6 (0-based index) with 'f'.
  • Choose two adjacent letters and swap them. For example, she can change "topcoder" to "tocpoder" by swapping characters 2 and 3 (0-based indices). She cannot change "topcoder" to "topdocer" because the characters being swapped must be adjacent.
Return the minimal number of operations required to convert S to T.

Definition

Class:
EllysString
Method:
theMin
Parameters:
String[], String[]
Returns:
int
Method signature:
int theMin(String[] s, String[] t)
(be sure your method is public)

Constraints

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

Examples

  1. {"usagi"}
    {"unagi"}
    Returns: 1
    Replace character 1 with 'n'.
  2. {"unagi"}
    {"nugai"}
    Returns: 2
    In the first operation, swap characters 0 and 1 to get "nuagi". In the second operation, swap characters 2 and 3 to get "nugai".
  3. {"ellys", "string"}
    {"e", "ll", "ysst", "ring"}
    Returns: 0
    Don't forget to concatenate strings. Both S and T are "ellysstring", so no operation is required.
  4. {"fox"}
    {"xfo"}
    Returns: 2
    In the first operation, swap characters 1 and 2 to get "fxo". In the second operation, swap characters 0 and 1 to get "xfo".
  5. {"salmon"}
    {"camlon"}
    Returns: 2
    In the first operation, replace character 0 with 'c' to get "calmon". In the second operation, swap characters 2 and 3 to get "camlon".
  6. {"boajxuidva"}
    {"jcayduvida"}
    Returns: 6
  7. {"vykdnujyezbcbmnatipqfuxqmgkvtn"}
    {"qokbqyujeqcbwsinatupqfoegmvkdz"}
    Returns: 22
Back to all problems