The Weekly Challenge 349

Character Runs & Pathfinding Back to Origin!

Original Challenge Link

Task 1: Power String

"How Long Is the Streak? Measuring Repeated Characters!"

This week's first task asks us to find the "power" of a string — the maximum length of a contiguous substring that consists of a single repeated character. For example, in "hoorayyy" the answer is 3 because "yyy" at the end is the longest run of identical characters.

The Strategy: Walk through the string character by character, tracking the current run length. When the current character matches the previous one, increment the counter. Otherwise reset it to 1. Keep the maximum value seen so far. This is a single-pass O(n) scan.
Perl Implementation
sub power_string ($text) {
    ($text) = $STR_CHECK->($text);
    die 'Expected a non-empty string' if length $text == 0;
    my $max_run = 0;
    my $run_len = 0;
    my $prev    = undef;

    for my $char ( split //, $text ) {
        if ( defined $prev && $char eq $prev ) {
            ++$run_len;
        }
        else {
            $prev    = $char;
            $run_len = 1;
        }
        $max_run = $run_len if $run_len > $max_run;
    }

    return $max_run;
}
Python Implementation
def power_string(text: str) -> int:
    """Return the maximum run length of a repeated character."""
    if not text:
        raise ValueError("Expected a non-empty string")

    best = 0
    current = 0
    previous = ""

    for char in text:
        if char == previous:
            current += 1
        else:
            previous = char
            current = 1
        if current > best:
            best = current
    return best

Task 2: Meeting Point

"Home Again? Does the Path Cross (0,0)?"

Given a path string made of U (up), D (down), L (left) and R (right) steps, determine whether the path ever returns to the origin (0, 0) at any point along the walk — not just at the very end, but at any intermediate step.

The Strategy: Start at (0, 0) and process each instruction one by one, updating coordinates. After each step check whether x == 0 and y == 0. If we ever return to the origin, return true immediately. If we finish the path without hitting (0, 0), return false.
Perl Implementation
sub returns_to_origin ($path) {
    ($path) = $PATH_CHECK->($path);
    die 'Path must contain only U, D, L, or R'
      if $path =~ /[^UDLR]/;
    my ( $x, $y ) = ( 0, 0 );
    return 1 if $path eq '';

    for my $step ( split //, $path ) {
        if    ( $step eq 'U' ) { ++$y }
        elsif ( $step eq 'D' ) { --$y }
        elsif ( $step eq 'L' ) { --$x }
        elsif ( $step eq 'R' ) { ++$x }
        return 1 if $x == 0 && $y == 0;
    }

    return 0;
}
Python Implementation
def returns_to_origin(path: str) -> bool:
    """
    Return True if the path ever visits (0, 0) while processing instructions.
    """
    if not all(ch in "UDLR" for ch in path):
        raise ValueError("Path must contain only U, D, L, or R")
    if not path:
        return True

    x = y = 0
    directions = {
        'U': (0, 1),
        'D': (0, -1),
        'L': (-1, 0),
        'R': (1, 0),
    }
    for step in path:
        dx, dy = directions[step]
        x += dx
        y += dy
        if x == 0 and y == 0:
            return True
    return False