Peter’s solutions: week 372 — 4 May 2026

THE WEEKLY CHALLENGE
Strings of strings

The Perl Camel

Task 2

Largest substring

You are given a string. Write a script to return the length of the largest substring between two identical characters, excluding the two characters. Return -1 if there is no such substring.

Examples


Example 1
Input: $str = 'aaaaa'
Output: 3
For character 'a', we have substring 'aaa'.

Example 2
Input: $str = 'abcdeba'
Output: 5
For character 'a', we have substring 'bcdeb'.

Example 3
Input: $str = 'abbc'
Output: 0
For character 'b', we have substring ''.

Example 4
Input: $str = 'abcaacbc'
Output: 4
For character 'a', we have substring 'bca'.
For character 'b', we have substring 'caac'.
For character 'c', we have substring 'aacb'.

Example 5
Input: $str = 'laptop'
Output: 2
For character 'p', we have substring 'to'.

Analysis

In tasks involving the structure of a string there is always a dilemma between keeping the string as a Perl scalar or splitting it into an array of characters.

In this case I opted for the array solution as it seems neater to work with $string[$j] rather than substr($string, $j, 1), and I think for run-time efficiency they will be about equal.

My solution is two nested loops over the starting and ending points of a possible pair of bracketing characters - noting that the examples given allow for a zero-length bracketed substring - eg 'summer'.

Allegedly 'subdermatoglyphic' is the longest Englsh word with no repeated character, and it means 'under your fingertips'.

I did toy with the idea of using a fancy regular expression to solve the challenge, but quickly concluded that it would be less efficient and harder to understand, but others may prove me wrong.

Try it 

Try running the script with any input:



example: cylindrical

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-05-04
use utf8;     # Week 372 - task 2 - Largest substring
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

largest_substring('algebra');
largest_substring('parliament');
largest_substring('subdermatoglyphic');
largest_substring('panorama');
largest_substring('summer');
largest_substring('supercalifragilisticexpialidocious');

sub largest_substring {
    
    my (@string, @longest, $j, $k);
    
    # initialise
    @string = split('', $_[0]);
    @longest = (-1, '', '');
    
    # loop over possible starting and ending characters
    for $j (0 .. $#string - 1) {
        for $k ($j + 1 .. $#string) {
            
            # found a matching pair
            if ($string[$j] eq $string[$k]) {
                if ($k - $j - 1 > $longest[0]) {
                    @longest = ($k - $j - 1, join('', @string[$j + 1 .. $k - 1]), $string[$j]);
                }
            }
        }
    }
                    
    say qq[\nInput:  '] . join('', @string) . q['];
    say qq[Output: ] . ($longest[0] >= 0 ? qq[$longest[0] ('$longest[1]' bracketed by '$longest[2]')] : '-1');
}

11 lines of code

Output


Input:  'algebra'
Output: 5 ('lgebr' bracketed by 'a')

Input:  'parliament'
Output: 3 ('rli' bracketed by 'a')

Input:  'subdermatoglyphic'
Output: -1

Input:  'panorama'
Output: 5 ('noram' bracketed by 'a')

Input:  'summer'
Output: 0 ('' bracketed by 'm')

Input:  'supercalifragilisticexpialidocious'
Output: 32 ('upercalifragilisticexpialidociou' bracketed 
   by 's')

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain