Skip to content

feat : new dp algo added UniqueSubsequenceCount.java #5586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c7c3d8b
feat : new algo uniquesubseqcount
Tuhinm2002 Oct 5, 2024
d124680
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
d99c476
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 5, 2024
33f06a8
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
892e365
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
7858443
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
569cc1c
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
96a5566
Merge branch 'master' into master
Tuhinm2002 Oct 6, 2024
3b8d047
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
735077d
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
8819fdb
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
51db0c6
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
6533338
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
ed36d8a
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
13a3e7e
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
697c108
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
002d55f
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
1536eeb
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
5288a5a
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
827b4d2
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
c7bd78a
Merge branch 'master' into master
Tuhinm2002 Oct 8, 2024
574fcf4
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 8, 2024
a505357
Merge branch 'master' into master
alxkm Oct 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.thealgorithms.dynamicprogramming;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* Utility class to find the number of unique subsequences that can be
* produced from a given string.
*
* <p> This class contains static methods to compute the unique subsequence count
* using dynamic programming and recursion. It ensures that duplicate characters
* are not counted multiple times in the subsequences.</p>
*
* <p> Author: https://github.com/Tuhinm2002 </p>
*/
public final class UniqueSubsequencesCount {

/**
* Private constructor to prevent instantiation of this utility class.
* This class should only be used in a static context.
*
* @throws UnsupportedOperationException if attempted to instantiate.
*/
private UniqueSubsequencesCount() {
throw new UnsupportedOperationException("Utility class");
}

/**
* Finds the number of unique subsequences that can be generated from
* the given string.
*
* <p> This method initializes a dynamic programming (DP) array and invokes
* the recursive helper function to compute the subsequence count.</p>
*
* @param str the input string from which subsequences are generated
* @return the total count of unique subsequences
*/
public static int countSubsequences(String str) {

// DP array initialized to store intermediate results
int[] dp = new int[str.length() + 1];
Arrays.fill(dp, -1);

// Calls the recursive function to compute the result
return recursiveCall(str, 0, dp);
}

/**
* Recursive helper function to count the number of unique subsequences
* starting from the given index.
*
* <p> Uses a HashSet to avoid counting duplicate characters within
* a single subsequence.</p>
*
* @param st the input string
* @param idx the current index from which to calculate subsequences
* @param dp dynamic programming array used to memoize results
* @return the total number of unique subsequences starting from the
* current index
*/
public static int recursiveCall(String st, int idx, int[] dp) {

// Base case: when index exceeds the string length
if (idx >= st.length()) {
return 0;
}

// If result is already calculated, return the memoized value
if (dp[idx] != -1) {
return dp[idx];
}

// Set to store characters to avoid duplicates
Set<Character> set = new HashSet<>();

int res = 0;

// Iterate over the string starting from current index
for (int j = idx; j < st.length(); j++) {

// If character is already in the set, skip it
if (set.contains(st.charAt(j))) {
continue;
}

// Add character to set and recursively calculate subsequences
set.add(st.charAt(j));

// 1 for the current subsequence + recursive call for the rest of the string
res = 1 + recursiveCall(st, j + 1, dp) + res;
}

// Memoize the result
dp[idx] = res;
return dp[idx];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class UniqueSubsequencesCountTest {

@Test
void subseqCountTestOne() {
String s = "abc";

assertEquals(7, UniqueSubsequencesCount.countSubsequences(s));
}

@Test
void subseqCountTestTwo() {
String s = "abcdashgdhas";

assertEquals(3592, UniqueSubsequencesCount.countSubsequences(s));
}

@Test
void subseqCountTestThree() {
String s = "a";

assertEquals(1, UniqueSubsequencesCount.countSubsequences(s));
}

@Test
void subseqCountTestFour() {
String s = "a b";

assertEquals(7, UniqueSubsequencesCount.countSubsequences(s));
}

@Test
void subseqCountTestFive() {
String s = "a1b2";

assertEquals(15, UniqueSubsequencesCount.countSubsequences(s));
}

@Test
void subseqCountTesSix() {
String s = "AaBb";
assertEquals(15, UniqueSubsequencesCount.countSubsequences(s));
}

@Test
void subseqCountTesSeven() {
String s = "abab";
assertEquals(11, UniqueSubsequencesCount.countSubsequences(s));
}
}