The Weekly Challenge 348

Vowel Matching & Clock Arithmetic!

Original Challenge Link

Task 1: String Alike

"Even Split: Do Both Halves Have the Same Vowel Count?"

This week's first task gives us an even-length string and asks whether it can be split into two equal halves that each contain the same non-zero number of vowels. A vowel is any of A, E, I, O, U (case-insensitive).

The Strategy: Split the string in the middle and count vowels in each half using a simple transliteration or regex. Return true only when both counts are equal AND greater than zero. If either half has no vowels, or the counts differ, return false.
Perl Implementation
sub string_alike ($text) {
    ($text) = $STRING_CHECK->($text);
    my $length = length $text;
    die 'Expected an even-length string' if $length % 2;

    my $half         = $length / 2;
    my $first_half   = substr $text, 0, $half;
    my $second_half  = substr $text, $half;
    my $first_count  = ( $first_half  =~ tr/AEIOUaeiou// );
    my $second_count = ( $second_half =~ tr/AEIOUaeiou// );

    return $first_count > 0 && $first_count == $second_count;
}
Python Implementation
def string_alike(text: str) -> bool:
    """Return True if both halves have the same non-zero vowel count."""
    if len(text) % 2:
        raise ValueError("Expected an even-length string")

    half = len(text) // 2
    vowels = set("AEIOUaeiou")
    first_count = sum(1 for ch in text[:half] if ch in vowels)
    second_count = sum(1 for ch in text[half:] if ch in vowels)

    return first_count > 0 and first_count == second_count

Task 2: Convert Time

"Clock Jumper: Minimum Steps to the Target Time!"

Given source and target 24-hour clock stamps, find the minimum number of operations to convert one into the other. Each operation adds 1, 5, 15, or 60 minutes to the current time.

The Strategy: Convert both stamps to minutes since midnight, compute the difference (wrapping around midnight if needed), then use a greedy decomposition with the largest step sizes first. Because every duration divides the next larger one, this greedy approach is guaranteed optimal.
Perl Implementation
my @OPERATIONS = ( 60, 15, 5, 1 );

sub convert_time ( $source, $target ) {
    my $start = _minutes_from_midnight($source);
    my $end   = _minutes_from_midnight($target);
    my $diff  = $end - $start;
    $diff += 24 * 60 if $diff < 0;

    my $operations = 0;
    for my $step (@OPERATIONS) {
        $operations += int( $diff / $step );
        $diff %= $step;
    }
    return $operations;
}
Python Implementation
def convert_time(source: str, target: str) -> int:
    """Minimum operations (1, 5, 15, 60 min) to go from source to target."""
    start = _minutes_from_midnight(source)
    end = _minutes_from_midnight(target)
    diff = (end - start) % (24 * 60)

    operations = 0
    for step in [60, 15, 5, 1]:
        operations += diff // step
        diff %= step
    return operations