-
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 8 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
53 changes: 53 additions & 0 deletions
53
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,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 | ||
alxkm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
alxkm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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) { | ||
alxkm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
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]; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
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,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() { | ||
alxkm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
String s = "abc"; | ||
|
||
assertEquals(7, UniqueSubsequencesCount.subseqCount(s)); | ||
} | ||
|
||
@Test | ||
void subseqCountTestTwo() { | ||
alxkm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
String s = "abcdashgdhas"; | ||
|
||
assertEquals(3592, UniqueSubsequencesCount.subseqCount(s)); | ||
} | ||
|
||
@Test | ||
void subseqCountTestThree() { | ||
String s = "aaaaa"; | ||
|
||
assertEquals(5, UniqueSubsequencesCount.subseqCount(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.