Dsa
Strings
Hard
Restricted Subsequence Counting
Given two strings S and T, count the number of ways T can be formed as a subsequence of S with the additional constraint: no two characters selected from S to form the subsequence can be at adjacent indices in S.
Input Format:
Line 1: String S
Line 2: String T
Output Format:
An integer representing the total count.
Example:
Input:
banana
ana
Output:
2
Explanation: S="banana", T="ana". Possible indices in S: (0, 2, 4) and (0, 2, 5). (0, 1, 2) is not allowed because 0 and 1 are adjacent.
Key concepts
dynamic programmingsubsequencescombinatorics
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now