```typescript
export function lcs(a: string, b: string): string {
  if (a.length === 0 || b.length === 0) return "";

  const dp: number[][] = Array.from({ length: a.length + 1 }, () =>
    Array(b.length + 1).fill(0)
  );

  for (let i = 1; i <= a.length; i++) {
    for (let j = 1; j <= b.length; j++) {
      if (a[i - 1] === b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  let lcsLength = dp[a.length][b.length];
  let lcs: string[] = [];

  while (lcsLength > 0) {
    if (
      a[a.length - lcsLength] === b[b.length - lcsLength] &&
      dp[a.length - lcsLength + 1][b.length - lcsLength] ===
        dp[a.length - lcsLength][b.length - lcsLength]
    ) {
      lcs.push(a[a.length - lcsLength]);
      lcsLength--;
    } else if (dp[a.length - lcsLength + 1][b.length - lcsLength] > dp[
      a.length - lcsLength
    ][b.length - lcsLength + 1]) {
      lcsLength--;
    } else {
      b = b.slice(0, b.length - 1);
    }
  }

  return lcs.reverse().join("");
}
```