**Common Child**

### Problem Statement :

A string is said to be a child of a another string if it can be formed by deleting 0 or more characters from the other string. Given two strings of equal length, what's the longest string that can be constructed such that it is a child of both? For example, ABCD and ABDC have two children with maximum length 3, ABC and ABD. They can be formed by eliminating either the D or C from both strings. Note that we will not consider ABCD as a common child because we can't rearrange characters and ABCD ABDC. Function Description Complete the commonChild function in the editor below. It should return the longest string which is a common child of the input strings. commonChild has the following parameter(s): s1, s2: two equal length strings Input Format There is one line with two space-separated strings, s1 and s2. Constraints 1 <= | s1 | , | s2 | <= 5000 All characters are upper case in the range ascii[A-Z]. Output Format Print the length of the longest string s, such that is a child of both s1 and s2.

### Solution :

` ````
Solution in C :
In C :
#include<stdio.h>
#include<string.h>
char str1[5005],str2[5005];
int c[5005][5005];
int main()
{
int i,j,m,n;
scanf("%s %s",str1+1,str2+1);
m=strlen(str1+1);
n=strlen(str2+1);
for(i=1;i<=m;i++)c[i][0]=0;
for(j=0;j<=n;j++)c[0][j]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
if(str1[i]==str2[j])
c[i][j]=c[i-1][j-1]+1;
else if(c[i-1][j]>=c[i][j-1])
c[i][j]=c[i-1][j];
else
c[i][j]=c[i][j-1];
}
printf("%d\n",c[m][n]);
return 0;
}
```

` ````
Solution in C++ :
In C++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[5005][5005];
int main() {
string a, b;
cin >> a >> b;
for(int i = 0; i < a.size(); ++i)
for(int j = 0; j < b.size(); ++j)
if(a[i] == b[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
else
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
cout << dp[a.size()][b.size()];
return 0;
}
```

` ````
Solution in Java :
In Java ;
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 s = new Scanner(System.in);
String s1 = s.nextLine();
String s2 = s.nextLine();
int[][] lengths = new int[s1.length()+1][s2.length()+1];
for (int i = 1; i < lengths.length; i++) {
for (int j = 1; j < lengths.length; j++) {
if (s1.charAt(i-1) == s2.charAt(j-1)) {
lengths[i][j] = lengths[i-1][j-1] + 1;
}
else if (lengths[i][j-1] > lengths[i-1][j]) {
lengths[i][j] = lengths[i][j-1];
}
else {
lengths[i][j] = lengths[i-1][j];
}
}
}
System.out.println(lengths[lengths.length-1][lengths.length-1]);
}
}
```

` ````
Solution in Python :
In Python3 :
def llis(a, b):
m = len(a)
n = len(b)
prev = [0 for x in range(n + 1)]
for i in range(1, m + 1):
curr = [0 for x in range(n + 1)]
for j in range(1, n + 1):
curr[j] = max(prev[j], curr[j - 1])
if a[i - 1] == b[j - 1]:
curr[j] = max(curr[j], prev[j - 1] + 1)
prev = curr
return curr[n]
if __name__ == '__main__':
a = input().strip()
b = input().strip()
c = set(a) & set(b)
a = "".join([x for x in a if x in c])
b = "".join([x for x in b if x in c])
print(llis(a, b))
```

## View More Similar Problems

## Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →## No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →## Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →## Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →## Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →## Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →