-
Notifications
You must be signed in to change notification settings - Fork 20.5k
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
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 d124680
Update UniqueSubsequencesCount.java
Tuhinm2002 d99c476
Update UniqueSubsequencesCountTest.java
Tuhinm2002 33f06a8
Update UniqueSubsequencesCount.java
Tuhinm2002 892e365
Update UniqueSubsequencesCount.java
Tuhinm2002 7858443
Update UniqueSubsequencesCount.java
Tuhinm2002 569cc1c
Update UniqueSubsequencesCount.java
Tuhinm2002 96a5566
Merge branch 'master' into master
Tuhinm2002 3b8d047
Update UniqueSubsequencesCount.java
Tuhinm2002 735077d
Update UniqueSubsequencesCount.java
Tuhinm2002 8819fdb
Update UniqueSubsequencesCountTest.java
Tuhinm2002 51db0c6
Merge branch 'master' into master
Tuhinm2002 6533338
Merge branch 'master' into master
Tuhinm2002 ed36d8a
Update UniqueSubsequencesCount.java
Tuhinm2002 13a3e7e
Merge branch 'master' into master
Tuhinm2002 697c108
Update UniqueSubsequencesCountTest.java
Tuhinm2002 002d55f
Update UniqueSubsequencesCount.java
Tuhinm2002 1536eeb
Update UniqueSubsequencesCountTest.java
Tuhinm2002 5288a5a
Update UniqueSubsequencesCountTest.java
Tuhinm2002 827b4d2
Update UniqueSubsequencesCountTest.java
Tuhinm2002 c7bd78a
Merge branch 'master' into master
Tuhinm2002 574fcf4
Update UniqueSubsequencesCount.java
Tuhinm2002 a505357
Merge branch 'master' into master
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/main/java/com/thealgorithms/dynamicprogramming/UniqueSubsequencesCount.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/com/thealgorithms/dynamicprogramming/UniqueSubsequencesCountTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
alxkm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
String s = "abc"; | ||
|
||
assertEquals(7, UniqueSubsequencesCount.countSubsequences(s)); | ||
} | ||
|
||
@Test | ||
void subseqCountTestTwo() { | ||
alxkm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.