The Weekly Challenge - 348

TASK #1: String Alike
You are given a string of even length.

Write a script to find out whether the given string can be split into two halves of equal lengths, each with the same non-zero number of vowels.

#!/usr/bin/perl
use strict;
use warnings;

sub stringAlike {
    my $s = shift;
    die "Even-length string expected!" if length($s) % 2;
    
    my $h = length($s)/2;

    # counting trick with the transliteration operator
    # https://reiniermaliepaard.nl/perl/part-1/index.php?id=transliteration
    
    # number of vowels in first half
    my $f = substr($s,0,$h) =~ tr/AEIOUaeiou//;
    # number of vowels in second half
    my $t = substr($s,$h) =~ tr/AEIOUaeiou//;
    
    # Alternative
    # my $f = lc(substr($s,0,$h)) =~ tr/aeiou//;
    # my $t = lc(substr($s,$h)) =~ tr/aeiou//;
    
    # $f > 0 means: first half has at least one vowel
    # $f == $t means: first and second half have the same number of vowels
    $f > 0 && $f == $t;
}

printf "%d\n", stringAlike('textbook'); # false
printf "%d\n", stringAlike('book'); # true
printf "%d\n", stringAlike('AbCdEfGh'); # true
printf "%d\n", stringAlike('rhythmmyth'); # false
printf "%d\n", stringAlike('UmpireeAudio'); # false