Longest Common Substring
MediumP2Coding
Return the length of the longest substring (contiguous run of characters) that appears in both a and b. If they share no character, return 0.
Example 1
- Input
- a = "abcdxyz", b = "xyzabcd"
- Output
- 4
- Why
- "abcd" appears in both strings.
Example 2
- Input
- a = "zxabcdezy", b = "yzabcdezx"
- Output
- 6
- Why
- "abcdez" is the longest shared run.
Example 3
- Input
- a = "abc", b = "def"
- Output
- 0
Constraints
- 0 <= a.length, b.length <= 1000
- Both strings contain lowercase English letters.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 11. ⌘Enter runs, ⌘⇧Enter submits.