Pages

Saturday, 23 January 2016

HACKER RANK: Common Child

Problem Link: https://www.hackerrank.com/challenges/common-child

Summary:
Given 2 strings of equal length what's the longest string that can be constructed such that it is a child of both.

For example:

Sample Input 1:
HARRY
SALLY

Sample Output 1:
2

Longest possible string is "AY"

Sample Input 2:
ABCDEF
FBDAMN

Sample Output 2:
2

Longest possible string is "BD"

Topic:
Dynamic Programming Approach: https://en.wikipedia.org/wiki/Dynamic_programming
Longest String Subsequence: https://en.wikipedia.org/wiki/Longest_common_substring_problem
                                                http://www.geeksforgeeks.org/longest-common-substring/

My Logic:

Let string str1 = HARRY and str2 = SALLY
So this is how i solved this question using DP(Dynamic Programming).


To view My Solution:
https://github.com/shivam04/hackerrank/blob/master/common-child.java

7 comments:

  1. How do you get the notion of this, this is pretty simple yet glorious solution. I appreciate it.

    ReplyDelete
  2. Nice yar..
    1 question, how this hard thoughts come to in ur mind? ;) :)

    ReplyDelete
    Replies
    1. thanks :)
      well when i study dp for the first time i found this question next day so i made this solution may be my mind is set for dp that time :p

      Delete
  3. This Code will fail for HARRY and SALLY test case and also for AA and BB

    ReplyDelete