Connection Status:
Competition Arena > TwoStringMasks
SRM 392 · 2008-03-06 · by darnley · String Manipulation
Class Name: TwoStringMasks
Return Type: String
Method Name: shortestCommon
Arg Types: (string, string)
Problem Statement

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.

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)
"*"
"*"
Returns: ""

Submissions are judged against all 135 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class TwoStringMasks with a public method string shortestCommon(string s1, string s2) · 135 test cases · 2 s / 256 MB per case

Submitting as anonymous