Skip to the code
cf.
+50 XP

#633 Longest Shared Fragment of Two Files

hard

A plagiarism checker for code submissions compares two files, each flattened into a string, and reports the length of the longest piece of text that appears in both.

Write longestShared(a, b) that returns the length of the longest string that is a substring of both a and b. Return 0 if they share no character.

Example 1

Input: a = "xabcdy", b = "zzabcdq"
Output: 4

abcd appears in both.

Example 2

Input: a = "abc", b = "xyz"
Output: 0

No character is shared.

Constraints

  • 0 <= a.length, b.length <= 10^4
  • a and b contain lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).