JavaScript Program to Count Distinct Subsequences
Last Updated :
18 Apr, 2024
In this article, we are going to learn about Count Distinct Subsequences in JavaScript. Counting distinct subsequences refers to determining the number of unique non-empty subarrays or subsequence combinations within a given sequence or string, without repetition of elements or order.
Example:
Input : str = "gfg"
Output : 7
The seven distinct subsequences are "", "g", "f",
"gf", "fg", "gg" and "gfg"
We will explore all the above methods along with their basic implementation with the help of examples.
In this approach, we generate distinct subsequences of a given string using recursion. It stores them in a Set sn and counts them. It recursively includes and excludes each character, creating all possible subsequences and counting unique ones.
Syntax:
// Recursive Case
else {
// When a particular character is taken
op[j] = s[i];
subsequences(s, op, i + 1, j + 1);
// When a particular character isn't taken
subsequences(s, op, i + 1, j);
return;
};
Example: In this example, we generate all possible subsequences of the string “gfg” and store them in a set called sn. It then prints the size of the set, which is the count of distinct subsequences, and in this case, the output is the size of the set containing distinct subsequences of “gfg”.
Javascript
// Create an empty set to store the subsequences
let sn = new Set();
let m = 0;
// Function for generating the subsequences
function subsequences(s, op, i, j) {
// Base Case
if (i == m) {
op[j] = '\0';
// Insert each generated
// subsequence into the set
sn.add(op.join(""));
return;
}
// Recursive Case
else {
// When a particular character is taken
op[j] = s[i];
subsequences(s, op, i + 1, j + 1);
// When a particular character isn't taken
subsequences(s, op, i + 1, j);
return;
}
}
let str = "gfg";
m = str.length;
let n = Math.pow(2, m) + 1;
let op = new Array(n);
// Function Call
subsequences(str, op, 0, 0);
console.log(sn.size);
Approach 2: Using dynamic programming with a for…of loop
In this approach, we calculate the count of distinct subsequences for a given string s. It utilizes dynamic programming and a for…of loop to efficiently compute the count, excluding the empty subsequence, and returns the result.
Syntax:
for ( variable of iterableObjectName) {
...
}
Example: In this example, The distinctSubsequences function uses dynamic programming to count unique subsequences in the string “gfg.” It returns 7 as there are seven distinct subsequences: “g,” “f,” “gfg,” “gf,” “gg,” “fg,” and an empty string.
Javascript
function distinctSubsequences(s) {
const dp = new Array(s.length + 1).fill(0);
dp[0] = 1;
const lastValue = new Map();
let i = 1;
for (const char of s) {
dp[i] = 2 * dp[i - 1];
if (lastValue.has(char)) {
dp[i] -= dp[lastValue.get(char) - 1];
}
lastValue.set(char, i);
i++;
}
return dp[s.length];
}
let result = "gfg";
console.log(distinctSubsequences(result));
Approach 3: Bitmasking approach
In the bitmasking approach, represent each subsequence as a bitmask, where each bit corresponds to whether an element is included or not. Iterate through all possible bitmasks, counting unique ones to find distinct subsequences efficiently.
Example: In this example The function counts distinct subsequences of a string ‘s’ using a bitmasking approach, avoiding duplicates by storing results in a set.
JavaScript
function countDistinctSubsequences(s) {
let sn = new Set();
for (let mask = 0; mask < (1 << s.length); mask++) {
let subsequence = "";
for (let i = 0; i < s.length; i++) {
if ((mask & (1 << i)) !== 0) {
subsequence += s[i];
}
}
sn.add(subsequence);
}
return sn.size;
}
let str = "gfg";
console.log(countDistinctSubsequences(str));
Share your thoughts in the comments
Please Login to comment...