Skip to content

Commit

Permalink
Added codes for 22 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 22, 2024
1 parent b158f73 commit 1fcb314
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
64 changes: 64 additions & 0 deletions GeeksForGeeks/February/22-2-24/GFG.java
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);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/22-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*m)
Space complexity - O(n*m)
2 changes: 2 additions & 0 deletions LeetCode/February/22-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n + |trust|)
Space complexity - O(n)
19 changes: 19 additions & 0 deletions LeetCode/February/22-2-24/Solution.java
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;
}
}

0 comments on commit 1fcb314

Please sign in to comment.