-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
CommonChild.java
34 lines (32 loc) · 911 Bytes
/
CommonChild.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String s2=sc.nextLine();
char a[]=s1.toCharArray();
char b[]=s2.toCharArray();
int m=a.length;
int n=b.length;
int arr[][]=new int[m+1][n+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i==0||j==0)
arr[i][j]=0;
else{
if(a[i-1]==b[j-1]){
arr[i][j]=arr[i-1][j-1]+1;
}
else{
arr[i][j]=Math.max(arr[i-1][j],arr[i][j-1]);
}
}
}
}
System.out.println(arr[m][n]);
}
}