#C13302. Can Form Substring
Can Form Substring
Can Form Substring
You are given two strings, S1 and S2. Your task is to determine whether S1 can be formed by concatenating several substrings (i.e. contiguous subsequences) of S2 in any order. Each substring of S2 can be used multiple times if needed.
In other words, you need to check if there exists a sequence of substrings \(s_1, s_2, \dots, s_k\) from S2 such that:
[ S1 = s_1 + s_2 + \cdots + s_k ]
Note:
- If either S1 or S2 is empty, the answer is
False
. - If S1 contains characters not present in S2, then it is impossible to form S1 and the answer should be
False
.
Examples:
- For
S1 = "applebananaapple"
andS2 = "bananapple"
, the answer isTrue
. - For
S1 = "applerapple"
andS2 = "apple"
, the answer isFalse
. - For
S1 = "12345"
andS2 = "54321"
, the answer isTrue
.
inputFormat
The input is given via stdin and consists of two lines:
- The first line contains the string S1.
- The second line contains the string S2.
outputFormat
Print a single line to stdout containing either True
or False
, indicating whether S1 can be formed by concatenating substrings of S2 as described.
applebananaapple
bananapple
True