Given a string s, you need to find the number of distinct non-empty subsequences that can be formed from this string.
A subsequence is formed by deleting some (possibly zero) characters from the original string while maintaining the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde", but "aec" is not.
Important: Two subsequences are considered different if they differ in at least one position. Since the answer can be extremely large, return the result modulo 109 + 7.
Example: For string "abc", the distinct subsequences are: "a", "b", "c", "ab", "ac", "bc", "abc" โ Total: 7
Input & Output
Visualization
Time & Space Complexity
Single pass through the string, with constant work per character
Fixed size array of 26 elements for tracking characters a-z
Constraints
- 1 โค s.length โค 2000
- s consists of lowercase English letters only
- Return the answer modulo 109 + 7