Last Updated: 8 Sep, 2025

Unique Longest Common Substring

Moderate
Asked in company
Frugal Testing

Problem statement

You are given two strings, s1 and s2, consisting of lowercase English letters. Your task is to find the length of the longest "unique common substring".

A substring sub is considered a unique common substring if it satisfies all of the following conditions:

1) sub is a substring of s1.


2) sub is a substring of s2.


3) sub appears exactly once within s1.


4) sub appears exactly once within s2.


If no such substring exists, the answer is 0.


Input Format:
The first line of input contains the string s1.
The second line of input contains the string s2.


Output Format:
Print a single integer representing the length of the longest unique common substring.


Note:
A substring is a contiguous block of characters. For example, "app" is a substring of "apple", but "ale" is not.