Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
80 changes: 36 additions & 44 deletions LCS.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
import java.util.*;
public class LCS
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
int l1 = a.length();
int l2 = b.length();
int dp[][] = new int[l1+1][l2+1];
int i,j;
for(i=0;i<=l1;i++)
dp[i][0] = 0;
for(i=0;i<=l2;i++)
dp[0][i] = 0;
StringBuffer s = new StringBuffer();
for(i=0;i<l1;i++)
{
for(j=0;j<l2;j++)
{
if(a.charAt(i)==b.charAt(j))
dp[i+1][j+1] = 1+dp[i][j];
else
dp[i+1][j+1] = Math.max(dp[i+1][j],dp[i][j+1]);
}
}
i = l1;
j = l2;
while(i>0 && j>0)
{
if(dp[i][j]!=dp[i][j-1])
{
s = s.append(Character.toString(b.charAt(j-1)));
i = i-1;
j = j-1;
}
else
{
j--;
}
}
// System.out.println(dp[l1][l2]); length of LCS
System.out.println(s.reverse());
}
public class LCS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
int l1 = a.length();
int l2 = b.length();
int dp[][] = new int[l1 + 1][l2 + 1];
int i, j;
for (i = 0; i <= l1; i++)
dp[i][0] = 0;
for (i = 0; i <= l2; i++)
dp[0][i] = 0;
StringBuffer s = new StringBuffer();
for (i = 0; i < l1; i++) {
for (j = 0; j < l2; j++) {
if (a.charAt(i) == b.charAt(j))
dp[i + 1][j + 1] = 1 + dp[i][j];
else
dp[i + 1][j + 1] = Math.max(dp[i + 1][j], dp[i][j + 1]);
}
}
i = l1;
j = l2;
while (i > 0 && j > 0) {
if (dp[i][j] != dp[i][j - 1]) {
s = s.append(Character.toString(b.charAt(j - 1)));
i = i - 1;
j = j - 1;
} else {
j--;
}
}
// System.out.println(dp[l1][l2]); length of LCS
System.out.print(s.reverse());
}
}
41 changes: 41 additions & 0 deletions coin_change.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Recursive java program for
// coin change problem.
import java.io.*;

class GFG {

// Returns the count of ways we can
// sum S[0...m-1] coins to get sum n
static int count( int S[], int m, int n ) {
// If n is 0 then there is 1 solution
// (do not include any coin)
if (n == 0)
return 1;

// If n is less than 0 then no
// solution exists
if (n < 0)
return 0;

// If there are no coins and n
// is greater than 0, then no
// solution exist
if (m <= 0 && n >= 1)
return 0;

// count is sum of solutions (i)
// including S[m-1] (ii) excluding S[m-1]
return count( S, m - 1, n ) +
count( S, m, n - S[m - 1] );
}

// Driver program to test above function
public static void main(String[] args) {
int arr[] = {1, 2, 3};
int m = arr.length;
System.out.println( count(arr, m, 4));


}

}