Task 1: Count Vowel
You are given a string.
Write a script to return all possible vowel substrings in the given string. A vowel substring is a substring that only consists of vowels and has all five vowels present in it.
Example 1
Input: $str = "aeiou"
Output: ("aeiou")
Example 2
Input: $str = "aaeeeiioouu"
Output: ("aaeeeiioou", "aeeeiioou", "aeeeiioouu")
Example 3
Input: $str = "aeiouuaxaeiou"
Output: ("aeiou", "aeiou", "eiouua")
Example 4
Input: $str = "uaeiou"
Output: ("aeiou", "uaeio")
Example 5
Input: $str = "aeioaeioa"
Output: ()
Logic
For each starting position in the string, extend rightward while characters are vowels. Track which vowels have been seen. Once all five vowels are present, record the substring. Continue extending to find all valid substrings from that start position.
Perl Solution
ch-1.pl
#!/usr/bin/env perl
use v5.38;
use warnings;
use feature 'signatures';
no warnings 'experimental::signatures';
sub count_vowel ($str) {
my @chars = split //, $str;
my @results;
for my $i (0 .. $#chars) {
next unless $chars[$i] =~ /[aeiou]/;
my %seen;
for my $j ($i .. $#chars) {
last unless $chars[$j] =~ /[aeiou]/;
$seen{$chars[$j]} = 1;
if (keys %seen == 5) {
push @results, substr($str, $i, $j - $i + 1);
}
}
}
return @results;
}
Python Solution
ch-1.py
def count_vowel(text: str) -> list[str]:
vowels = set("aeiou")
results = []
chars = list(text)
for i in range(len(chars)):
if chars[i] not in vowels:
continue
seen: set[str] = set()
for j in range(i, len(chars)):
if chars[j] not in vowels:
break
seen.add(chars[j])
if len(seen) == 5:
results.append("".join(chars[i:j + 1]))
return results
Task 2: Largest Same-digits Number
You are given a string containing 0-9 digits only.
Write a script to return the largest number with all digits the same in the given string.
Example 1
Input: $str = "6777133339"
Output: 3333
Example 2
Input: $str = "1200034"
Output: 4
Example 3
Input: $str = "44221155"
Output: 55
Example 4
Input: $str = "88888"
Output: 88888
Example 5
Input: $str = "11122233"
Output: 222
Logic
Find all consecutive runs of identical digits. Convert each run to a number and return the largest one. For example, "000" = 0, "4" = 4, so "4" wins. This handles leading zeros correctly.
Perl Solution
ch-2.pl
#!/usr/bin/env perl
use v5.38;
use warnings;
use feature 'signatures';
no warnings 'experimental::signatures';
sub largest_same_digits ($str) {
return '' if $str eq '';
my @runs;
my $cur = '';
for my $ch (split //, $str) {
if ($cur eq '' || substr($cur, -1) eq $ch) {
$cur .= $ch;
} else {
push @runs, $cur;
$cur = $ch;
}
}
push @runs, $cur if $cur ne '';
my $best = '';
for my $run (@runs) {
my $run_val = $run + 0;
my $best_val = $best + 0;
if ($run_val > $best_val || ($run_val == $best_val && length($run) > length($best))) {
$best = $run;
}
}
return $best;
}
Python Solution
ch-2.py
import re
def largest_same_digits(text: str) -> str:
if not text:
return ""
runs = re.findall(r'((\d)\2*)', text)
best = ""
for run, _ in runs:
if (int(run) > int(best) if best else True):
best = run
return best