Lcs algorithm implementation