Skip to content

Commit 7891d78

Browse files
committed
longest common subsequence
1 parent c14e72e commit 7891d78

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

src/BinaryTree/PrintPostFromPreInOrder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ private void start() {
2323
}
2424

2525
private void buildTree(int start, int end) {
26-
if (start > end ) {
26+
if (start > end) {
2727
return;
2828
}
2929
int data = preorder[preIndex++];
3030
int pos = findPos(data);
3131
buildTree(start, pos - 1);
3232
buildTree(pos + 1, end);
33-
System.out.print(data+" ");
33+
System.out.print(data + " ");
3434
}
3535

3636
private int findPos(int data) {

src/DP/LcS.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package DP;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Created by dheeraj on 7/9/2016.
7+
*/
8+
public class LCS {
9+
10+
private static Integer sol[][];
11+
12+
private static int findLis(String one, String two, int start, int end) {
13+
if(start < 0 || start > one.length()-1 || end > two.length()-1 || end < 0){
14+
return 0;
15+
}
16+
if (sol[start][end] != null) {
17+
return sol[start][end];
18+
}
19+
int a = findLis(one, two, start - 1, end - 1);
20+
int b = findLis(one, two, start - 1, end);
21+
int c = findLis(one, two, start, end - 1);
22+
23+
int ans = one.charAt(start) == two.charAt(end) ? a + 1 : Math.max(b, c);
24+
sol[start][end] = ans;
25+
return ans;
26+
}
27+
28+
public static void main(String[] strings) {
29+
String one = "abcd";
30+
String two = "abcxd";
31+
32+
sol = new Integer[one.length()][two.length()];
33+
int ans = findLis(one, two, one.length() - 1, two.length() - 1);
34+
System.out.println(ans);
35+
36+
Stack<Character> characters = new Stack<Character>();
37+
int x = one.length()-1;
38+
int y = two.length()-1;
39+
while(x >= 0 && y >=0){
40+
41+
if(x-1 >= 0 && sol[x][y] == sol[x-1][y]){
42+
x--;
43+
}else if(y-1 > 0 && sol[x][y] == sol[x][y-1]){
44+
y--;
45+
}else{
46+
characters.push(one.charAt(x));
47+
x--;y--;
48+
}
49+
}
50+
while(!characters.empty()){
51+
System.out.print(characters.pop()+" ");
52+
}
53+
}
54+
55+
}

src/heap/KMostFrequent.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* Created by dheeraj on 7/8/2016.
77
*/
88
public class KMostFrequent {
9-
10-
static Data[] datas = new Data[4];
11-
static int size;
9+
private static HashMap<String, Data> wordCountMap = new HashMap<String, Data>();
10+
private static Data[] datas = new Data[4];
11+
private static int size;
1212

1313
private static void addToHeap(Data data) {
1414
if (size < datas.length) {
@@ -91,8 +91,6 @@ public String toString() {
9191
}
9292
}
9393

94-
private static HashMap<String, Data> wordCountMap = new HashMap<String, Data>();
95-
9694
private static void addWord(String word) {
9795
Data data = wordCountMap.get(word);
9896
if (data == null) {

0 commit comments

Comments
 (0)