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 8 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,53 @@
package com.thealgorithms.dynamicprogramming;

// Program to find the number of Subsequences can be produced from a string

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

// Author -> https://github.com/Tuhinm2002

public final class UniqueSubsequencesCount {

private UniqueSubsequencesCount() {
throw new UnsupportedOperationException("Utility class");
}

public static int subseqCount(String str) {

int[] dp = new int[str.length() + 1];
Arrays.fill(dp, -1);

return recursiveCall(str, 0, dp);
}

public static int recursiveCall(String st, int idx, int[] dp) {

if (idx >= st.length()) {
return 0;
}

if (dp[idx] != -1) {
return dp[idx];
}

Set<Character> set = new HashSet<>();

int res = 0;

for (int j = idx; j < st.length(); j++) {

if (set.contains(st.charAt(j))) {
continue;
}

set.add(st.charAt(j));

res = 1 + recursiveCall(st, j + 1, dp) + res;
}

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

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

import org.junit.jupiter.api.Test;

public final class UniqueSubsequencesCountTest {

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

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

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

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

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

assertEquals(5, UniqueSubsequencesCount.subseqCount(s));
}
}