Distinct Subsequences šŸŽÆ

beginner
20 min

Distinct Subsequences šŸŽÆ

Welcome to our in-depth guide on Distinct Subsequences! This lesson is perfect for both beginners and intermediate learners who are interested in data structures and algorithms. Let's dive right in!

What are Distinct Subsequences? šŸ“

In the context of strings, a subsequence is a sequence that can be derived from another sequence by deleting some elements (possibly none) without changing the order of the remaining elements. A distinct subsequence is a subsequence where each element is unique.

Let's take the example of the string "ABAC". The distinct subsequences are:

  • "A", "B", "C", "AB", "AC", "BC", "ABC"

Why are Distinct Subsequences Important? šŸ’”

Distinct Subsequences have numerous applications in computer science, particularly in problems related to strings, dynamic programming, and algorithms. They help us to solve complex problems efficiently and effectively.

Algorithm for Distinct Subsequences šŸ“

The algorithm for finding distinct subsequences is based on dynamic programming. We'll use a 2D array dp where dp[i][j] represents the number of distinct subsequences of the first i characters of the string that end with the jth character.

Here's a step-by-step breakdown of the algorithm:

  1. Initialize dp with all zeros:
python
n = len(str) dp = [[0 for _ in range(len(str))] for _ in range(n+1)]
  1. Fill in the first row and column:
python
for i in range(1, n+1): dp[i][str[i-1]] = 1
  1. Fill in the rest of the dp array:
python
for i in range(1, n+1): for j in range(1, n+1): if str[i-1] != str[j-1]: dp[i][j] = dp[i-1][j] + dp[i][j-1] else: dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1]
  1. The number of distinct subsequences is stored in dp[n][n-1].

Code Examples šŸ’”

Let's see two code examples in Python and JavaScript.

Python

python
def distinct_subsequences(str): n = len(str) dp = [[0 for _ in range(len(str))] for _ in range(n+1)] for i in range(1, n+1): dp[i][str[i-1]] = 1 for i in range(1, n+1): for j in range(1, n+1): if str[i-1] != str[j-1]: dp[i][j] = dp[i-1][j] + dp[i][j-1] else: dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1] return dp[n][n-1] print(distinct_subsequences("ABAC")) # Output: 11

JavaScript

javascript
function distinctSubsequences(str) { const n = str.length; const dp = Array.from({length: n+1}, () => Array(n+1).fill(0)); for(let i = 1; i < n+1; i++) { dp[i][str[i-1]] = 1; } for(let i = 1; i < n+1; i++) { for(let j = 1; j < n+1; j++) { if (str[i-1] !== str[j-1]) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } else { dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1]; } } } return dp[n][n-1]; } console.log(distinctSubsequences("ABAC")); // Output: 11

Quiz šŸ’”

Quick Quiz
Question 1 of 1

Which part of the `dp` array stores the number of distinct subsequences for a given string?

That's it for our in-depth guide on Distinct Subsequences! We hope this lesson has been helpful. If you have any questions or need further clarification, feel free to ask. Happy coding! šŸ’»