BackmediumStringsPhonePePayPal

Identifying Pattern Substrings Solution

Problem Statement

Given a string and a pattern, find the length of the longest substring that occurs at least twice in the string and also contains the given pattern as a substring.

Example 1
Input
string = 'abcabcabcabc', pattern = 'abc'
Output
4

Explanation: Step-by-step: 1. Find all substrings of the string. 2. Filter out substrings that do not contain the pattern. 3. Find the longest substring that occurs at least twice in the string and also contains the given pattern as a substring.

Example 2
Input
string = 'ababab', pattern = 'ab'
Output
6

Explanation: Step-by-step: 1. Find all substrings of the string. 2. Filter out substrings that do not contain the pattern. 3. Find the longest substring that occurs at least twice in the string and also contains the given pattern as a substring.

Constraints

  • The length of the input string is at most 1000 characters.
  • The length of the pattern is at least 1 character and at most 100 characters.
  • The input string and pattern only contain lowercase English letters.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Identifying Pattern Substrings — Problem Statement & Solution Guide

StringsMediumFundamentals
TimeO(n^2)
|
SpaceO(n)

Problem Description

Given a string and a pattern, find the length of the longest substring that occurs at least twice in the string and also contains the given pattern as a substring.

Examples

Example 1

Input

string = 'abcabcabcabc', pattern = 'abc'

Output

4

Explanation: Step-by-step: 1. Find all substrings of the string. 2. Filter out substrings that do not contain the pattern. 3. Find the longest substring that occurs at least twice in the string and also contains the given pattern as a substring.

Example 2

Input

string = 'ababab', pattern = 'ab'

Output

6

Explanation: Step-by-step: 1. Find all substrings of the string. 2. Filter out substrings that do not contain the pattern. 3. Find the longest substring that occurs at least twice in the string and also contains the given pattern as a substring.

Constraints

  • The length of the input string is at most 1000 characters.
  • The length of the pattern is at least 1 character and at most 100 characters.
  • The input string and pattern only contain lowercase English letters.

Optimal Approach & Strategy

The optimal approach involves using a hashmap to store the frequency of each substring and a sliding window to generate substrings, reducing the time complexity to O(n^2).

Brute Force Approach

A naive approach is to generate all possible substrings of the input string and check each one to see if it contains the given pattern and occurs at least twice, resulting in a time complexity of O(n^3). This approach is inefficient for large strings.

Asked in Top Tech Interviews

PhonePePayPal

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.