> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/l/longest_common_subsequence.md).

# Longest common subsequence

```ruby
func lcs(xstr, ystr) is cached {
 
    xstr.is_empty && return xstr
    ystr.is_empty && return ystr
 
    var(x, xs, y, ys) = (xstr.first(1), xstr.slice(1),
                         ystr.first(1), ystr.slice(1))
 
    if (x == y) {
        x + lcs(xs, ys)
    } else {
        [lcs(xstr, ys), lcs(xs, ystr)].max_by { .len }
    }
}
 
say lcs("thisisatest", "testing123testing")
```

#### Output:

```
tsitest
```
