Subsequence Determination Problem

Your task is to verify if the given string STR1 is a subsequence of the string STR2. A subsequence means characters from STR2 are retained in their original order but some (or none) characters are deleted.

Input:

The first line of input contains an integer ‘T’ denoting the number of test cases.
Then for each test case, two separate lines contain:
1. String ‘STR1’ representing the potential subsequence.
2. String ‘STR2’ in which to check for subsequence.

Output:

For each test case, output ‘True’ if ‘STR1’ is a subsequence of ‘STR2’, otherwise ‘False’.

Example:

Input:
3
BAE
ABADE
ACE
ABCDE
ADB
ABCDE

Output:
True
True
False
Explanation:

In the first test case, ‘BAE’ is formed by deleting some characters from ‘ABADE’.
In the second test case, ‘ACE’ is formed from ‘ABCDE’ by removing certain characters without rearranging them.
In the third test case, ‘ADB’ cannot be formed from ‘ABCDE’ due to the order mismatch.

Constraints:

  • 1 <= T <= 50
  • 1 <= N, M <= 10^4
  • Where N and M denote the lengths of STR1 and STR2 respectively.
  • Strings consist only of English uppercase letters.
  • Time limit: 1 second.
Note:
You are not required to print the output explicitly, implementation of the function suffices.
AnswerBot
1mo

Verify if a string is a subsequence of another string by checking if characters are retained in order.

  • Iterate through both strings simultaneously, checking if characters match in order.

  • If a character ...read more

Help your peers!
Add answer anonymously...
Paytm Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter