-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonChild.java
More file actions
39 lines (32 loc) · 964 Bytes
/
Copy pathCommonChild.java
File metadata and controls
39 lines (32 loc) · 964 Bytes
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
35
36
37
38
39
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int commonChild(String s1, String s2){
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
int lastMatchI = -1;
int lastMatchJ = -1;
int len = 0;
for (int i = lastMatchI + 1; i < c1.length; i++) {
for (int j = lastMatchJ + 1; j < c2.length; j++) {
if (c1[i] == c2[j]) {
lastMatchI = i;
lastMatchJ = j;
len++;
break;
}
}
}
return len;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s1 = in.next();
String s2 = in.next();
int result = commonChild(s1, s2);
System.out.println(result);
}
}