Common Child

  • + 1 comment

    I could not understand this test case in the Run: 'SHINCHAN' vs 'NOHARAAA'. I can only see HA as the common child, so my anwser is 2 instead of the offical answer 3.

    Attached is my code in Python3:

    def commonChild(s1, s2):
        '''
        find the closest match from beginning
        '''
        for i in range(0, min(len(s1), len(s2))):
            for j in range(i+1):
                if s1[i] == s2[j]:
                    cnt = 1
                    for k in range(1, min(len(s1) - i, len(s2) - j)):
                        if s1[i+k] != s2[j+k]:
                            break
                        cnt += 1
                    return cnt + commonChild(s1[i+cnt:], s2[j+cnt:])
                if s1[j] == s2[i]:
                    cnt = 1
                    for k in range(1, min(len(s1) - j, len(s2) - i)):
                        if s1[j+k] != s2[i+k]:
                            break
                        cnt += 1
                    return cnt + commonChild(s1[j+cnt:], s2[i+cnt:])
        return 0