Peter’s blog ✴ Week 349 ✴ 24 November 2025

THE WEEKLY CHALLENGE
Powering to the origin

The Perl Camel

Task 1

Power string

You are given a string. Write a script to return the power of the given string. The power of the string is the maximum length of a non-empty substring that contains only one unique character.

Examples


Example 1
Input: $str = 'textbook'
Output: 2
Breakdown: 't', 'e', 'x', 'b', 'oo', 'k'
The longest substring with one unique character is 'oo'.

Example 2
Input: $str = 'aaaaa'
Output: 5

Example 3
Input: $str = 'hoorayyy'
Output: 3
Breakdown: 'h', 'oo', 'r', 'a', 'yyy'
The longest substring with one unique character is 'yyy'.

Example 4
Input: $str = 'x'
Output: 1

Example 5
Input: $str = 'aabcccddeeffffghijjk'
Output: 4
Breakdown: 'aa', 'b', 'ccc', 'dd', 'ee', 'ffff', 'g', 'h',
   'i', 'jj', 'k'
The longest substring with one unique character is 'ffff'.

Analysis

This is another challenge that allows for a wide range of different solutions.

Mine is to start by looking for $chars = length($string) consecutive identical characters using m|(.)\1{$chars}| and then decrementing $chars by 1 until I find a match.

It would be more efficient to step along the string incrementing $max if this character matches the previous one or setting $max to 1 if not, but it's hard to think of a use case where efficiency would matter.

Perl Weekly’s review

from PW issue 749

This post demonstrates creative problem-solving with elegant regex decrementing for Task 1 and a clever eval-based dispatch system for Task 2. Peter shows strong analytical thinking by carefully distinguishing between final-position and intermediate-position checks, and makes practical engineering trade-offs between cleverness and performance.

This review may cover either or both challenges for this week.

I am sorry that the 'Try it' feature is currently working very slowly or not at all owing to some issue with my web hosting provider.

Try it 

Try running the script with any input:



example: spoon moon moooo glue

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2025-11-24
use utf8;     # Week 349 - task 1 - Power string
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

power_string('textbook');
power_string('zzzzz');
power_string('abcde');
power_string('xoooxooooxoooooxoooo');

sub power_string {
    
    my ($string, $chars);
    
    # initialise
    $string = shift;    
    $chars = length($string);
    
    # decrement chars while consecutive characters
    while ($chars-- > 1) {
        last if $string =~ m|(.)\1{$chars}|;
    }
    
    say qq[\nInput:  '$string'];
    say qq[Output: ] . ($chars + 1);
}

8 lines of code

Output from script


Input:  'textbook'
Output: 2

Input:  'zzzzz'
Output: 5

Input:  'abcde'
Output: 1

Input:  'xoooxooooxoooooxoooo'
Output: 5

 

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