TopcoderArchiveVisit Topcoder
SRM6 Mar 2008
SRM 392

TwoStringMasks

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given two Strings s1 and s2. Each string contains some letters and exactly one asterisk ('*').

You must replace each asterisk with a sequence of letters (possibly of zero length). The resulting two strings must be equal.

Return the shortest possible resulting string. If it is impossible to make the strings equal, return "impossible" (quotes for clarity) instead.

Definition

Class:
TwoStringMasks
Method:
shortestCommon
Parameters:
String, String
Returns:
String
Method signature:
String shortestCommon(String s1, String s2)
(be sure your method is public)

Constraints

  • s1 will contain between 1 and 50 characters, inclusive.
  • s2 will contain between 1 and 50 characters, inclusive.
  • s1 and s2 will contain only uppercase letters ('A'-'Z') and asterisks ('*').
  • s1 and s2 will contain exactly one asterisk each.

Examples

  1. "TOPC*DER"
    "T*PCODER"
    Returns: "TOPCODER"
    Each of the asterisks should be replaced with an "O".
  2. "HELLO*"
    "HI*"
    Returns: "impossible"
    No matter how you replace the asterisks, the second characters of the strings will differ. So it is impossible to make the strings equal.
  3. "GOOD*LUCK"
    "*"
    Returns: "GOODLUCK"
    The first asterisk should be replaced with an empty string.
  4. "*SAMPLETEST"
    "THIRDSAMPLE*"
    Returns: "THIRDSAMPLETEST"
  5. "*TOP"
    "*CODER"
    Returns: "impossible"
  6. "*"
    "A*"
    Returns: "A"
  7. "*A"
    "B*"
    Returns: "BA"
  8. "LASTCASE*"
    "*LASTCASE"
    Returns: "LASTCASE"
Back to all problems