TopcoderArchiveVisit Topcoder
SRM25 Jun 2013
SRM 597

LittleElephantAndString

Problem statement, definition, constraints, and public examples.

Problem Statement

Little Elephant from the Zoo of Lviv likes strings.

You are given a String A and a String B of the same length. In one turn Little Elephant can choose any character of A and move it to the beginning of the string (i.e., before the first character of A). Return the minimal number of turns needed to transform A into B. If it's impossible, return -1 instead.

Definition

Class:
LittleElephantAndString
Method:
getNumber
Parameters:
String, String
Returns:
int
Method signature:
int getNumber(String A, String B)
(be sure your method is public)

Constraints

  • A will contain between 1 and 50 characters, inclusive.
  • B will contain between 1 and 50 characters, inclusive.
  • A and B will be of the same length.
  • A and B will consist of uppercase letters ('A'-'Z') only.

Examples

  1. "ABC"
    "CBA"
    Returns: 2
    The optimal solution is to make two turns. On the first turn, choose character 'B' and obtain string "BAC". On the second turn, choose character 'C' and obtain "CBA".
  2. "A"
    "B"
    Returns: -1
    In this case, it's impossible to transform A into B.
  3. "AAABBB"
    "BBBAAA"
    Returns: 3
  4. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "ZYXWVUTSRQPONMLKJIHGFEDCBA"
    Returns: 25
  5. "A"
    "A"
    Returns: 0
  6. "DCABA"
    "DACBA"
    Returns: 2
Back to all problems