Skip to content

Adding-Algorithms #22

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 1 commit into from
Oct 3, 2018
Merged
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
38 changes: 38 additions & 0 deletions data-structures/UseStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Stack
{
int stcksize=100;
int stck[] = new int[stcksize];
int tos;
Stack()
{
tos=-1;
}
void push(int item)
{
if(tos==stcksize-1)
System.out.println("Stack Overflow");
else
stck[++tos]=item;
}
int pop()
{
if(tos<0)
{
System.out.println("Stack Underflow.");
return 0;
}
else
return stck[tos--];
}
}
public class UseStack
{
public static void main(String args[])
{
Stack s=new Stack();
s.push(1);
s.push(2);
s.push(3);
s.pop(); // return 3 as it was the last inserted element
}
}
45 changes: 45 additions & 0 deletions dynamic-programming/LongestCommonSubsequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Finding Longest Common Subsequence of Two Strings Using Dynamic Programming
import java.util.Scanner;
public class LongestCommonSubsequence
{
int lcs(char[] X, char[] Y, int m, int n)
{
int DP[][] = new int[m+1][n+1];
// Bottom-Up Approach for dp
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
{
if(i==0 || j==0)
DP[i][j]=0;
else if(X[i-1]==Y[j-1])
DP[i][j]=DP[i-1][j-1]+1;
else
DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
}
}
return DP[m][n];
}
int max(int a, int b)
{
if(a>b)
return a;
else
return b;
}
public static void main(String[] args)
{
LongestCommonSubsequence obj=new LongestCommonSubsequence();
String s1="",s2="";
Scanner scan=new Scanner(System.in);
System.out.println("Enter 1st String");
s1=scan.next();
System.out.println("Enter 2nd String");
s2=scan.next();
char[] X=s1.toCharArray();
char[] Y=s2.toCharArray();
int m=X.length;
int n=Y.length;
System.out.println("Length of Longest Common Subsequence is: "+obj.lcs(X,Y,m,n));
}
}