Longest Common Subsequence

Given two nonempty lowercase strings str1 and str2, return the maximum length of a sequence that appears in both in the same order. You may skip characters,…

Given two nonempty lowercase strings str1 and str2, return the maximum length of a sequence that appears in both in the same order. You may skip characters, but you may not reorder or reuse a character position. The sequence need not be contiguous. Return its length, not a reconstructed sequence.

- 1 <= character length of each operand <= 1000. - Only lowercase ASCII letters a through z. - The visualizer accepts each operand up to 8 characters; longer valid inputs are coding-only.

Example 1: input str1 = "planet", str2 = "painting"; output 4. planet and painting share p, a, n, t in that order, giving length 4. There is no fifth character that can be placed in the same relative order in both.

Example 2: input str1 = "abca", str2 = "acba"; output 3. The stated character and ordering conditions determine this length.

Example 3: input str1 = "aaaa", str2 = "bbbb"; output 0. No valid shared match satisfies the stated ordering and character conditions.

Category: Strings. Topic: dynamic-programming. Difficulty: hard. Patterns: dynamic-programming.