Skip to content

2981. Find Longest Special Substring That Occurs Thrice I #937

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use a brute force approach due to the small constraints of s (length of up to 50). We'll:

  1. Iterate over possible lengths of substrings (from longest to shortest).
  2. Check all substrings of the given length and count their occurrences.
  3. If a substring occurs at least three times, check if it is special (made of one repeated character).
  4. Return the length of the longest such substring. If no substring satisfies the conditions, return -1.

Let's implement this solution in PHP: 2981. Find Longest Special Substring That Occurs Thrice I

<?php
/**
 * @param String $s
 * @return Integer
 */
function maximumLength($s) {
    $n = strlen($s);

    // Iterate over substring lengths from longest to…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Dec 10, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Dec 10, 2024
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants