DAY 6

4.1

9 votes
Dynamic programming, String, Medium, Algorithms, Dynamic Programming, Counting and Arrangements
Problem

You are given a string of length N and two alphabets x and y. The string consists of only lowercase English alphabets.
You have to count the total number of distinct sub-sequences of the string starting with x and ending with y.
Note:

  • Two sub-sequences are said to be distinct if a particular index of string is used in one but not in another.
  • As the count can be very large, so you have print it modulo with 109+7.

Input format
First line contains an integer N.
Second line contains a string of length N.
Third line contains two space-separated alphabets x and y.

Output format
Print a single integer representing the number of sub-sequences modulo 109+7.

Constraints
1N105
String consists of only lowercase English letters.
x and y are lowercase English letters.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The subsequences are:
1. ab (using index 0,1)
2. ab (using index 0,3)
3. abb
4. acb
5. abcb

Editor Image

?