#K86942. Subsequence Rearrangement

    ID: 36976 Type: Default 1000ms 256MiB

Subsequence Rearrangement

Subsequence Rearrangement

You are given two strings, ( S_1 ) and ( S_2 ). Determine whether ( S_2 ) can be obtained as a subsequence of ( S_1 ). In other words, check if there exists a sequence of indices ( i_1 < i_2 < \cdots < i_k ) in ( S_1 ) such that ( S_1[i_1]S_1[i_2]\cdots S_1[i_k] = S_2 ).

For example:

  • If ( S_1 = \texttt{ahbgdc} ) and ( S_2 = \texttt{abc} ), the answer is True because ( S_2 ) can be obtained by taking the characters at indices 1, 3, and 5 (1-indexed).
  • If ( S_1 = \texttt{ahbgdc} ) and ( S_2 = \texttt{axc} ), the answer is False because ( \texttt{x} ) does not appear in ( S_1 ) following the necessary order.

    Note that the solution should read the input from standard input (stdin) and write the output to standard output (stdout).

inputFormat

The input consists of two lines. The first line contains the string ( S_1 ) and the second line contains the string ( S_2 ). Both strings consist of only printable characters and may be empty.

outputFormat

Output a single line: True if ( S_2 ) is a subsequence of ( S_1 ), or False otherwise.## sample

ahbgdc
abc
True