Skip to content

ORIGINAL CODING QUESTION

Shared glyph subsequence length

Return the length of the longest sequence that is a subsequence of both inputs. Subsequence order is preserved but indexes may be skipped.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n, n glyph integers, m, m glyph integers. Print the requested integer.

Constraints

1 ≤ n,m ≤ 30; each glyph integer is in [0,25].

Example 1

Input

3 0 1 2 2 0 2

Output

2

Glyphs 0,2 form a length-two shared subsequence.

Example 2

Input

2 0 1 2 2 3

Output

0

No shared glyph exists.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

A shared final glyph can extend a shared shorter prefix.

Hint 2

When final glyphs differ, drop one side’s final glyph.

Hint 3

Build a table for pairs of prefix lengths.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →