Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.function.*;
- import java.util.regex.*;
- import java.util.stream.*;
- import static java.util.stream.Collectors.joining;
- import static java.util.stream.Collectors.toList;
- class Result {
- /*
- * Complete the 'commonChild' function below.
- *
- * The function is expected to return an INTEGER.
- * The function accepts following parameters:
- * 1. STRING s1
- * 2. STRING s2
- */
- public static int commonChild(String s1, String s2) {
- // Write your code here
- int result = 0;
- for (int i = 0; i < s1.length(); i++) {
- int first = maxChild(s1, s2, i, 0);
- int second = maxChild(s2, s1, i, 0);
- if (first > result) {
- result = first;
- }
- if (second > result) {
- result = second;
- }
- }
- return result;
- }
- public static int maxChild(String s1, String s2, int i1, int i2) {
- if (i1 >= s1.length() || i2 >= s2.length()) {
- return 0;
- }
- char c1 = s1.charAt(i1);
- int found = s2.indexOf(c1, i2);
- if (found == -1) {
- return 0;
- }
- int maxChild = 0;
- int subsequent;
- for (int i = i1+1; i < s1.length(); i++) {
- subsequent = maxChild(s1, s2, i, found + 1);
- if (subsequent > maxChild) {
- maxChild = subsequent;
- }
- //System.out.println(subsequent + " " + c1);
- }
- return maxChild + 1;
- }
- }
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
- String s1 = bufferedReader.readLine();
- String s2 = bufferedReader.readLine();
- int result = Result.commonChild(s1, s2);
- bufferedWriter.write(String.valueOf(result));
- bufferedWriter.newLine();
- bufferedReader.close();
- bufferedWriter.close();
- }
- }
Add Comment
Please, Sign In to add comment