[DAY14]Common Child

Sherlock Chiou
1 min readOct 17, 2019

--

Solution: Dynamic programming is used to solve this problem. We need to create a table to keep track of longest common child. We set characters of string number one as rows and characters of string number one as columns. For each row, we traverse through the column. If we met a same character, we set the value of this element equals to the upper left corner’s value plus one. Which means this longer common child extends from it. Other than this, if we are in position x row(s) and y column(s). We would set the value of matrix[x-1][y] to matrix[x][y], if matrix[x-1][y] is greater than matrix[x][y-1], and vice versa.

Supplementary Material: Video below is very concise and well explained.

--

--

Sherlock Chiou
Sherlock Chiou

Written by Sherlock Chiou

Be Effective and Optimize for Learning

No responses yet