-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b158f73
commit 1fcb314
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,64 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
|
||
class Distinct_Occurrences | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
sc.nextLine(); | ||
while(t>0) | ||
{ | ||
String line = sc.nextLine(); | ||
String S = line.split(" ")[0]; | ||
String T = line.split(" ")[1]; | ||
|
||
Solution ob = new Solution(); | ||
System.out.println(ob.subsequenceCount(S,T)); | ||
t--; | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
/*You are required to complete this method*/ | ||
|
||
class Solution | ||
{ | ||
int mod=(int)(1e9+7); | ||
|
||
int memoization(int i,int j,String s,String t,int dp[][]) | ||
{ | ||
if(j==t.length()) | ||
return 1; | ||
|
||
if(i==s.length()) | ||
return 0; | ||
|
||
if(dp[i][j]!=-1) | ||
return dp[i][j]; | ||
|
||
int include=0; | ||
|
||
if(s.charAt(i)==t.charAt(j)) | ||
include=memoization(i+1,j+1,s,t,dp); | ||
|
||
int exclude = memoization(i+1,j,s,t,dp); | ||
|
||
return dp[i][j]=(include+exclude)%mod; | ||
} | ||
|
||
|
||
int subsequenceCount(String s, String t) | ||
{ | ||
// Your code here | ||
int dp[][]=new int[s.length()][t.length()]; | ||
|
||
for(int[] a:dp) | ||
Arrays.fill(a,-1); | ||
|
||
return memoization(0,0,s,t,dp); | ||
} | ||
} |
This file contains 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,2 @@ | ||
Time complexity - O(n*m) | ||
Space complexity - O(n*m) |
This file contains 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,2 @@ | ||
Time complexity - O(n + |trust|) | ||
Space complexity - O(n) |
This file contains 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,19 @@ | ||
class Solution | ||
{ | ||
public int findJudge(int n, int[][] trust) | ||
{ | ||
int[] count = new int[n + 1]; | ||
|
||
for (int[] t : trust) | ||
{ | ||
--count[t[0]]; | ||
++count[t[1]]; | ||
} | ||
|
||
for (int i = 1; i < n + 1; ++i) | ||
if (count[i] == n - 1) | ||
return i; | ||
|
||
return -1; | ||
} | ||
} |