{
  "schema_version": 2,
  "id": "commands/lcs",
  "title": "LCS",
  "url": "https://redis.io/docs/latest/commands/lcs/",
  "summary": "Finds the longest common substring.",
  "since": "7.0.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-06-15T06:26:34-07:00",
  "page_type": "content",
  "content_hash": "c2c1d3120325a28021392e61c3d438ad0a0fc7f685c1b646bca43ce9fb6cf913",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "The LCS command implements the longest common subsequence algorithm. Note that this is different than the longest common string algorithm, since matching characters in the string does not need to be contiguous.\n\nFor example, the `LCS` between \"foo\" and \"fao\" is \"fo\", since scanning the two strings from left to right, the longest common set of characters is composed of the first \"f\" and then the \"o\".\n\nLCS is very useful in order to evaluate how similar two strings are. Strings can represent many things. For instance if two strings are DNA sequences, the LCS will provide a measure of similarity between the two DNA sequences. If the strings represent some text edited by some user, the LCS could represent how different the new text is compared to the old one, and so forth.\n\nNote that this algorithm runs in `O(N*M)` time, where N is the length of the first string and M is the length of the second string. So either use a separate Redis instance to run this algorithm, or make sure to run it against very small strings."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key1</code></summary>\n\nThe name of the first key.\n\n</details>\n\n<details open><summary><code>key2</code></summary>\n\nThe name of the second key.\n\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "<details open><summary><code>LEN</code></summary>\n\nReturn the length of the longest common subsequence instead of the subsequence itself.\n\n</details>\n\n<details open><summary><code>IDX</code></summary>\n\nReturn the positions of the matches instead of the matching string.\n\n</details>\n\n<details open><summary><code>MINMATCHLEN min-match-len</code></summary>\n\nWhen used with `IDX`, only return matches at least `min-match-len` characters long.\n\n</details>\n\n<details open><summary><code>WITHMATCHLEN</code></summary>\n\nWhen used with `IDX`, include the length of each match in the result.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "[code example]\n\nSometimes you need just the length of the match:\n\n[code example]\n\nHowever what is often very useful is to know the match position in each string:\n\n[code example]\n\nMatches are produced from the last one to the first one, since this is how\nthe algorithm works, and it more efficient to emit the matches in the same order.\nThe above array means that the first match (second element of the array)\nis between positions 2-3 of the first string and 0-1 of the second.\nThen there is another match between 4-7 and 5-8.\n\nTo restrict the list of matches to the ones of a given minimal length:\n\n[code example]\n\nFinally, you may want to get the match length as well:\n\n[code example]"
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Bulk string reply](../../develop/reference/protocol-spec#bulk-strings): the longest common subsequence.\n* [Integer reply](../../develop/reference/protocol-spec#integers): the length of the longest common subsequence when _LEN_ is given.\n* [Array reply](../../develop/reference/protocol-spec#arrays): an array with the LCS length and all the ranges in both the strings when _IDX_ is given.\n\n**RESP3:**\n\nOne of the following:\n* [Bulk string reply](../../develop/reference/protocol-spec#bulk-strings): the longest common subsequence.\n* [Integer reply](../../develop/reference/protocol-spec#integers): the length of the longest common subsequence when _LEN_ is given.\n* [Map reply](../../develop/reference/protocol-spec#maps): a map with the LCS length and all the ranges in both the strings when _IDX_ is given."
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "plaintext",
      "code": "> MSET key1 ohmytext key2 mynewtext\nOK\n> LCS key1 key2\n\"mytext\"",
      "section_id": "examples"
    },
    {
      "id": "examples-ex1",
      "language": "plaintext",
      "code": "> LCS key1 key2 LEN\n(integer) 6",
      "section_id": "examples"
    },
    {
      "id": "examples-ex2",
      "language": "plaintext",
      "code": "> LCS key1 key2 IDX\n1) \"matches\"\n2) 1) 1) 1) (integer) 4\n         2) (integer) 7\n      2) 1) (integer) 5\n         2) (integer) 8\n   2) 1) 1) (integer) 2\n         2) (integer) 3\n      2) 1) (integer) 0\n         2) (integer) 1\n3) \"len\"\n4) (integer) 6",
      "section_id": "examples"
    },
    {
      "id": "examples-ex3",
      "language": "plaintext",
      "code": "> LCS key1 key2 IDX MINMATCHLEN 4\n1) \"matches\"\n2) 1) 1) 1) (integer) 4\n         2) (integer) 7\n      2) 1) (integer) 5\n         2) (integer) 8\n3) \"len\"\n4) (integer) 6",
      "section_id": "examples"
    },
    {
      "id": "examples-ex4",
      "language": "plaintext",
      "code": "> LCS key1 key2 IDX MINMATCHLEN 4 WITHMATCHLEN\n1) \"matches\"\n2) 1) 1) 1) (integer) 4\n         2) (integer) 7\n      2) 1) (integer) 5\n         2) (integer) 8\n      3) (integer) 4\n3) \"len\"\n4) (integer) 6",
      "section_id": "examples"
    }
  ]
}
