Timer is Running

Number of distinct subsequences
Submissions: 6   Accuracy:


  Difficulty: Easy   Marks: 2

Given a string consisting of lower case English alphabets, the task is to find the number of distinct subsequences of the string

Note: Answer can be very large, so, ouput will be answer modulo 109+7

Example 1:

Input: s = "gfg"
Output: 7
Explanation: The seven distinct
subsequences are "", "g", "f", "gf", "fg",
"gg" and "gfg" 

Example 2:

Input: s = "ggg"
Output: 4
Explanation: The four distinct
subsequences are "", "g", "gg", "ggg"

Your task:
You do not need to read any input or print anything. The task is to complete the function distinctSubsequences(), which takes a string as input and returns an integer.

Expected Time Complexity: O(|str|)
Expected Auxiliary Space: O(|str|)

1 ≤ |s| ≤ 105
s contains lower case English alphabets

** For More Input/Output Examples Use 'Expected Output' option **

Author: Hemang Sarkar

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.