TopCoder problem "TwoStringMasks" used in SRM 392 (Division I Level One , Division II Level Two)



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

0)
    
"TOPC*DER"
"T*PCODER"
Returns: "TOPCODER"
Each of the asterisks should be replaced with an "O".
1)
    
"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.
2)
    
"GOOD*LUCK"
"*"
Returns: "GOODLUCK"
The first asterisk should be replaced with an empty string.
3)
    
"*SAMPLETEST"
"THIRDSAMPLE*"
Returns: "THIRDSAMPLETEST"
4)
    
"*TOP"
"*CODER"
Returns: "impossible"
5)
    
"*"
"A*"
Returns: "A"
6)
    
"*A"
"B*"
Returns: "BA"
7)
    
"LASTCASE*"
"*LASTCASE"
Returns: "LASTCASE"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=11126&pm=8706

Writer:

darnley

Testers:

PabloGilberto , Olexiy , ivan_metelsky , ged

Problem categories:

String Manipulation