The Weekly Challenge - 254

TASK #2: Reverse Vowels
You are given a string S of lower case letters 'a'..'z'.

Write a script that finds the pair of consecutive letters in S that appears most frequently. If there is more than one such pair, chose the one that is the lexicographically first.

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

sub reverse_vowels {
    my ($s) = @_;
   
    my @chars = split //, $s;
    my @vowel_index = grep { $chars[$_] =~ /[aeiouAEIOU]/ } 0..$#chars;

    my $i = 0;
    my $j = $#vowel_index;

    while ($i < $j) {
        @chars[@vowel_index[$i, $j]] = @chars[@vowel_index[$j, $i]];
        $i++;
        $j--;
    }

    return ucfirst(lc join '', @chars);
}

# Tests

my $s;

# Example 1
$s = "Raku";
print reverse_vowels($s),"\n"; # Output: Ruka

# Example 2
$s = "Julia";
print reverse_vowels($s),"\n"; # Output: Jaliu

# Example 3
$s = "Uiua";
print reverse_vowels($s),"\n"; #  Output: Auiu