Skip to content

Commit ada7d05

Browse files
authored
Merge pull request #22 from descifrado/hello
Adding-Algorithms
2 parents e4e2695 + 26690e6 commit ada7d05

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

data-structures/UseStack.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Stack
2+
{
3+
int stcksize=100;
4+
int stck[] = new int[stcksize];
5+
int tos;
6+
Stack()
7+
{
8+
tos=-1;
9+
}
10+
void push(int item)
11+
{
12+
if(tos==stcksize-1)
13+
System.out.println("Stack Overflow");
14+
else
15+
stck[++tos]=item;
16+
}
17+
int pop()
18+
{
19+
if(tos<0)
20+
{
21+
System.out.println("Stack Underflow.");
22+
return 0;
23+
}
24+
else
25+
return stck[tos--];
26+
}
27+
}
28+
public class UseStack
29+
{
30+
public static void main(String args[])
31+
{
32+
Stack s=new Stack();
33+
s.push(1);
34+
s.push(2);
35+
s.push(3);
36+
s.pop(); // return 3 as it was the last inserted element
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Finding Longest Common Subsequence of Two Strings Using Dynamic Programming
2+
import java.util.Scanner;
3+
public class LongestCommonSubsequence
4+
{
5+
int lcs(char[] X, char[] Y, int m, int n)
6+
{
7+
int DP[][] = new int[m+1][n+1];
8+
// Bottom-Up Approach for dp
9+
for(int i=0;i<=m;i++)
10+
{
11+
for(int j=0;j<=n;j++)
12+
{
13+
if(i==0 || j==0)
14+
DP[i][j]=0;
15+
else if(X[i-1]==Y[j-1])
16+
DP[i][j]=DP[i-1][j-1]+1;
17+
else
18+
DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
19+
}
20+
}
21+
return DP[m][n];
22+
}
23+
int max(int a, int b)
24+
{
25+
if(a>b)
26+
return a;
27+
else
28+
return b;
29+
}
30+
public static void main(String[] args)
31+
{
32+
LongestCommonSubsequence obj=new LongestCommonSubsequence();
33+
String s1="",s2="";
34+
Scanner scan=new Scanner(System.in);
35+
System.out.println("Enter 1st String");
36+
s1=scan.next();
37+
System.out.println("Enter 2nd String");
38+
s2=scan.next();
39+
char[] X=s1.toCharArray();
40+
char[] Y=s2.toCharArray();
41+
int m=X.length;
42+
int n=Y.length;
43+
System.out.println("Length of Longest Common Subsequence is: "+obj.lcs(X,Y,m,n));
44+
}
45+
}

0 commit comments

Comments
 (0)