TSECJ103 - Editorial

PROBLEM LINK:

Contest

Author: Full name

DIFFICULTY:

Easy-Medium

PROBLEM:

Find if a string S splits completely into two strings s1 and s2, the order of characters in s1 and s2 being same as that in S.

QUICK EXPLANATION:

Given a string S made out of s1 and s2, the first character of s must be the first character of s1 or s2. If this isn’t true, the answer is NO. If it is true, we can check from the next index of S and the remaining portion of s1 and s2.

EXPLANATION:

We can consider a recursive solution where the base cases are:
“” can be formed out of “” and “” → answer is YES.
If S is “” but s1 and s2 are not null, then S does not split completely into s1 and s2 → answer is NO.
If S is “” but s1 and s2 are not null, then S does not split completely into s1 and s2 → answer is NO.

For the recursion, if S[0]==s1[0], answer could be recursively obtained by considering substring of S and s1. Similarly for s2. If the first character of S does not equal p1[0] or p2[0], the answer is NO.

AUTHOR’S SOLUTIONS:

Author’s solution can be found here.

I wrote a recursive solution too.