In this post, we will solve HackerRank Common Child Problem Solution.

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. Letters cannot be rearranged. Given two strings of equal length, what’s the longest string that can be constructed such that it is a child of both?**Example**

s1 = ‘ABCD’

s2= ’ABDC

These strings have two children with maximum length 3, ABC and ABD. They can be formed by eliminating either the D or C from both strings. Return 3.

**Function Description**

Complete the *commonChild* function in the editor below.

commonChild has the following parameter(s):

*string s1:*a string*string s2:*another string

**Returns**

*int:*the length of the longest string which is a common child of the input strings

**Input Format**

There are two lines, each with a string, s1 and s2.

**Sample Input**

```
HARRY
SALLY
```

**Sample Output**

` 2`

**Explanation**

The longest string that can be formed by deleting zero or more characters from HARRY and SALLY is AY, whose length is 2.

**Sample Input 1**

```
AA
BB
```

**Sample Output 1**

`0`

**Explanation 1**

AA and BB have no characters in common and hence the output is 0.

**Sample Input 2**

```
SHINCHAN
NOHARAAA
```

**Sample Output 2**

`3`

**Explanation 2**

The longest string that can be formed between SHINCHAN and NOHARAAA while maintaining the order is NHA.

**Sample Input 3**

```
ABCDEF
FBDAMN
```

**Sample Output 3**

`2`

**Explanation 3**

BD is the longest child of the given strings.

## Common Child C Solution

```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int process(char *str1,int i1,char *str2,int i2,int **dp);
int main(){
char str1[5001],str2[5002];
int len,**dp,i,j;
scanf("%s%s",str1,str2);
len=strlen(str1);
dp=(int**)malloc(len*sizeof(int*));
for(i=0;i<len;i++)
dp[i]=(int*)malloc(len*sizeof(int));
for(i=0;i<len;i++)
for(j=0;j<len;j++)
dp[i][j]=-1;
printf("%d",process(str1,len-1,str2,len-1,dp));
return 0;
}
int process(char *str1,int i1,char *str2,int i2,int **dp){
int len1,len2;
if(i1==-1 || i2==-1)
return 0;
if(dp[i1][i2]!=-1)
return dp[i1][i2];
if(str1[i1]==str2[i2])
dp[i1][i2]=process(str1,i1-1,str2,i2-1,dp)+1;
else{
len1=process(str1,i1-1,str2,i2,dp);
len2=process(str1,i1,str2,i2-1,dp);
dp[i1][i2]=(len1>len2)?len1:len2;
}
return dp[i1][i2];
}
```

## Common Child C++ Solution

```
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[5001][5001];
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
string s1, s2;
cin >> s1 >> s2;
int lg = 0;
for(int i = 0; i < s1.size(); ++i) {
for(int j = 0; j < s2.size(); ++j) {
if (s1[i] == s2[j]) {
dp[i+1][j+1] = dp[i][j] + 1;
lg = max(lg, dp[i+1][j+1]);
} else {
dp[i+1][j+1] = max(max(dp[i][j], dp[i+1][j]), dp[i][j+1]);
}
}
}
cout << lg << endl;
return 0;
}
```

## Common Child C Sharp Solution

```
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static int LCSLength(string str1, string str2)
{
if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
return 0;
if (str1[0] == str2[0])
return 1 + LCSLength(str1.Substring(1), str2.Substring(1));
else
{
int len1 = LCSLength(str1, str2.Substring(1));
int len2 = LCSLength(str1.Substring(1), str2);
return Math.Max(len1, len2);
}
}
static int Solve(string str1, string str2)
{
return LCSLength(str1, str2);
}
static int Solve2(string str1, string str2)
{
var L = new int[str1.Length + 1, str2.Length + 1];
for (int i = str1.Length - 1; i >= 0; --i)
{
for (int j = str2.Length - 1; j >= 0; --j)
{
if (str1[i] == str2[j])
{
L[i,j] = 1 + L[i+1, j+1];
}
else
{
L[i,j] = Math.Max(L[i, j+1], L[i+1, j]);
}
}
}
return L[0,0];
}
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
string str1 = Console.ReadLine();
string str2 = Console.ReadLine();
Console.WriteLine(Solve2(str1, str2));
}
}
```

## Common Child Java Solution

```
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 a, String b){
int[][] C = new int[a.length()+1][b.length()+1];
for (int i = 0; i < a.length(); i++) {
for (int j = 0; j < b.length(); j++) {
if (a.charAt(i) == b.charAt(j)) {
C[i+1][j+1] = C[i][j] + 1;
} else {
C[i+1][j+1] = Math.max(C[i+1][j], C[i][j+1]);
}
}
}
for (int i = 0; i < a.length(); i++) {
}
return C[a.length()][b.length()];
}
}
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();
}
}
```

## Common Child JavaScript Solution

```
function processData(input) {
input = input.split('\n');
var X = input[0];
var Y = input[1];
var m = X.length;
var n = Y.length;
var L = [];
for (var i = 0; i <= m; i++) {
L[i] = [];
for (var j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
}
}
console.log(L[m][n]);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
```

## Common Child Python Solution

```
s1 = input().strip()
s2 = input().strip()
n = len(s1)
assert n == len(s2)
currow = [0]*(n+1)
#print(len(tbl),[len(row) for row in tbl])
for i in range(n):
s1i = s1[i]
prevrow = currow
currow = [0]
cr = 0
for s2j, nr, nr1 in zip(s2, prevrow, prevrow[1:]):
if s1i == s2j:
cr = 1 + nr
else:
if cr < nr1:
cr = nr1
currow.append(cr)
print(currow[-1])
```

Other Solutions

Greetings! Very helpful advice in this particular article! It is the little changes which will make the most important changes. Thanks a lot for sharing!

I’m amazed, I must say. Seldom do I come across a blog that’s equally educative and interesting, and let me tell you, you have hit the nail on the head. The problem is something that too few people are speaking intelligently about. I’m very happy that I came across this in my search for something regarding this.

An impressive share! I’ve just forwarded this onto a friend who was conducting a little homework on this. And he in fact bought me dinner due to the fact that I found it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanks for spending time to discuss this issue here on your site.

Excellent blog post. I definitely appreciate this site. Stick with it!

Way cool! Some extremely valid points! I appreciate you penning this write-up plus the rest of the website is very good.

Hello! I could have sworn I’ve been to this blog before but after going through a few of the posts I realized it’s new to me. Regardless, I’m certainly delighted I came across it and I’ll be bookmarking it and checking back often!

Pretty! This was an extremely wonderful post. Thank you for providing this information.

Great info. Lucky me I discovered your website by chance (stumbleupon). I’ve book-marked it for later!

I used to be able to find good info from your blog articles.

I couldn’t refrain from commenting. Exceptionally well written.

Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It will always be interesting to read through content from other writers and practice a little something from their websites.

Hello, I do believe your website could be having internet browser compatibility issues. Whenever I look at your web site in Safari, it looks fine but when opening in I.E., it’s got some overlapping issues. I merely wanted to provide you with a quick heads up! Aside from that, wonderful site!

An impressive share! I have just forwarded this onto a coworker who has been doing a little homework on this. And he actually bought me breakfast simply because I discovered it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending the time to discuss this topic here on your site.

Everything is very open with a precise clarification of the issues. It was truly informative. Your website is very useful. Thank you for sharing!

After going over a handful of the articles on your web site, I honestly appreciate your technique of writing a blog. I added it to my bookmark website list and will be checking back soon. Please visit my website as well and tell me your opinion.

Hi there! This post could not be written much better! Reading through this post reminds me of my previous roommate! He always kept talking about this. I most certainly will send this article to him. Fairly certain he will have a great read. Many thanks for sharing!

After checking out a number of the articles on your web page, I seriously appreciate your technique of writing a blog. I book marked it to my bookmark site list and will be checking back in the near future. Please visit my website too and tell me your opinion.

This page truly has all of the info I wanted about this subject and didn’t know who to ask.

I really love your blog.. Excellent colors & theme. Did you create this amazing site yourself? Please reply back as I’m looking to create my own blog and would like to find out where you got this from or exactly what the theme is called. Appreciate it!

I’m impressed, I must say. Seldom do I encounter a blog that’s both equally educative and engaging, and without a doubt, you’ve hit the nail on the head. The issue is something too few people are speaking intelligently about. I am very happy that I found this in my search for something regarding this.

I really like looking through an article that can make people think. Also, many thanks for permitting me to comment.

You’re so cool! I do not think I’ve truly read a single thing like this before. So wonderful to find someone with original thoughts on this subject matter. Seriously.. many thanks for starting this up. This site is something that’s needed on the web, someone with some originality.

Aw, this was an incredibly good post. Taking the time and actual effort to create a top notch article… but what can I say… I hesitate a lot and don’t manage to get anything done.

This is a good tip particularly to those fresh to the blogosphere. Short but very precise info… Thank you for sharing this one. A must read post!

Way cool! Some very valid points! I appreciate you penning this article and also the rest of the site is also very good.

You made some decent points there. I checked on the web to learn more about the issue and found most individuals will go along with your views on this site.

It’s hard to come by educated people in this particular topic, however, you sound like you know what you’re talking about! Thanks

I needed to thank you for this wonderful read!! I absolutely enjoyed every bit of it. I’ve got you book marked to look at new stuff you post…