Connection Status:
Competition Arena > InsertZ
SRM 587 · 2013-06-25 · by ir5 · Simple Search, Iteration, String Manipulation
Class Name: InsertZ
Return Type: String
Method Name: canTransform
Arg Types: (string, string)
Problem Statement

Problem Statement

You are given two Strings: init and goal. Both init and goal contain lowercase letters only. Additionally, init does not contain the character 'z'.

Your goal is to transform init into goal. The only operation you are allowed to do is to insert the character 'z' anywhere into init. You can repeat the operation as many times as you want, and each time you can choose any position where to insert the 'z'.

For example, if init="fox", you can transform it to "fzox" in one operation. Alternately, you can transform "fox" into "zzzfoxzzz" in six operations. It is not possible to transform "fox" into any of the strings "fx", "foz", "fxo", "foxy".

Return "Yes" (quotes for clarity) if it is possible to transform init into goal. Otherwise, return "No".

Notes

  • Note that the return value is case sensitive.

Constraints

  • init and goal will each contain between 1 and 50 characters, inclusive.
  • Each character of init and goal will be a lowercase letter ('a'-'z').
  • init will not contain the letter 'z'.
Examples
0)
"fox"
"fozx"
Returns: "Yes"

By inserting 'z' to the position bettween 'o' and 'x' in "fox", we obtain "fozx".

1)
"fox"
"zfzoxzz"
Returns: "Yes"

You may perform the operation multiple times.

2)
"foon"
"foon"
Returns: "Yes"

In this case init and goal are equal. You do not have to perform the operation.

3)
"topcoder"
"zopzoder"
Returns: "No"
4)
"aaaaaaaaaa"
"a"
Returns: "No"

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

Coding Area

Language: C++17 · define a public class InsertZ with a public method string canTransform(string init, string goal) · 111 test cases · 2 s / 256 MB per case

Submitting as anonymous