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
4mo

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!
Select
Add answer anonymously...

Top Software Engineer Interview Questions Asked at Paytm

Q. What are the differences between a HashMap and a Hashtable?
Q. How do the web and the internet work?
Q. Given a 2x2 matrix, modify it in such a way that if a cell contains 0, all the e...read more
Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits