Perl Weekly Challenge 377.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 377.
Task 1: Reverse Existence
Submitted by: Mohammad Sajid Anwar
You are given a string.
Write a script to find whether any substring of length 2 is
also present in the reverse of the given string.
Example 1
Input: $str = "abcba"
Output: true
Reverse of given string is "abcba".
The substring "ab" in original string is also present in the reverse string too.

Example 2
Input: $str = "racecar"
Output: true
The substring "ce" is present in both.

Example 3
Input: $str = "abcd"
Output: false

Example 4
Input: $str = "banana"
Output: true
The substring "an" is present in both.

Example 5
Input: $str = "hello"
Output: true
The substring "ll" is present in both.

I use a regular expression to extract all two character
subsequences, I reverse them and I test the original string
against all. Any match yields a true" result. If all
attempts fail, I get a =False result. The code fits a
1.5-liner.
Examples:
perl -E '
L:for(@ARGV){$s=$_;$s=~/$_/&&(say("$s -> T"),next L)for(map{"".reverse $_}
$s=~/(?=(..))/g);say "$s -> F";}
' abcba racecar abcd banana hello
Results:
abcba -> T
racecar -> T
abcd -> F
banana -> T
hello -> T
The full code is
1 # Perl weekly challenge 377
2 # Task 1: Reverse Existence
3 #
4 # See https://wlmb.github.io/2026/06/08/PWC377/#task-1-reverse-existence
5 use v5.36;
6 die <<~"FIN" unless @ARGV;
7 Usage: $0 S0 S1...
8 to test if the reversed string Sn when reversed
9 contains a two character substring of the original string.
10 FIN
11 STRING: for my $string (@ARGV){
12 for(map{"".reverse $_} # reverse each substring
13 $string=~/(?=(..))/g # get all 2 char substrings
14 ){
15 say("$string -> True"), next STRING
16 if $string=~/$_/;
17 }
18 say "$string -> False";
19 }
Example:
./ch-1.pl abcba racecar abcd banana hello
Results:
abcba -> True
racecar -> True
abcd -> False
banana -> True
hello -> True
Task 2: Prefix Suffix
Submitted by: Mohammad Sajid Anwar
You are given an array of strings.
Write a script to find if the two strings (str1, str2) in
the given array such that str1 is prefix and suffix of
str2. Return the total count of such pairs.
Example 1
Input: @array = ("a", "aba", "ababa", "aa")
Output: 4
$array[0], $array[1]: "a" is a prefix and suffix of "aba"
$array[0], $array[2]: "a" is a prefix and suffix of "ababa"
$array[0], $array[3]: "a" is a prefix and suffix of "aa"
$array[1], $array[2]: "aba" is a prefix and suffix of "ababa"

Example 2
Input: @array = ("pa", "papa", "ma", "mama")
Output: 2
$array[0], $array[1]: "pa" is a prefix and suffix of "papa"
$array[2], $array[3]: "ma" is a prefix and suffix of "mama"

Example 3
Input: @array = ("abao", "ab")
Output: 0

Example 4
Input: @array = ("abab", "abab")
Output: 1
$array[0], $array[1]: "abab" is a prefix and suffix of "abab"

Example 5
Input: @array = ("ab", "abab", "ababab")
Output: 3
$array[0], $array[1]: "ab" is a prefix and suffix of "abab"
$array[0], $array[2]: "ab" is a prefix and suffix of "ababab"
$array[1], $array[2]: "abab" is a prefix and suffix of "ababab"

Example 6
Input: @array = ("abc", "def", "ghij")
Output: 0

I can check if a string matches at the beginning and at the end of each of the strings and increment a counter each succesful match. As each string matches itself, I subtract the number of strings. I assume the arrays are input as space separated strings. The result fits a one-liner:
Examples:
perl -E '
for(@ARGV){my@s=split" ";my $c=0;for my $x(@s){/^$x/&&/$x$/&&++$c for@s};say"$_ -> ",$c - @s;}
' "a aba ababa aa" "pa papa ma mama" "abao ab" "abab abab" \
"ab abab ababab" "abc def ghij"
Results:
a aba ababa aa -> 4
pa papa ma mama -> 2
abao ab -> 0
abab abab -> 2 *
ab abab ababab -> 3
abc def ghij -> 0
There is certain ambiguity in the problem statement: Every
string is a prefix and suffix of itself, but the examples
show that doesn’t count. However, if we are given two equal
strings $s1==$s2 then $s1 is a prefix and suffix of
$s2 and $s2 is a prefix and suffix of $s1. So I
expect a contribution of 2 to the count. But then, in
example 3 the expected output is 1. This is confusing, as it
could happen that three strings are equal,
$s1==$s2==$s3. What would the expected solution be in this
case? Even if example 3 is correct, the result is
ambiguous. There are six ordered pairs of equal strings, 3
unordered pairs, so a result of 1 or 3 would be consistent
with example 3. According to my approach,
the result would be 6 and unambiguous. Thus, I keep that approach.
The full code is:
1 # Perl weekly challenge 377
2 # Task 2: Prefix Suffix
3 #
4 # See https://wlmb.github.io/2026/06/08/PWC377/#task-2-prefix-suffix
5 use v5.36;
6 die <<~"FIN" unless @ARGV;
7 Usage: $0 A0 A1...
8 to find in how many pairs of space-separated substrings of the string An
9 are the first is both prefix and suffix of the second.
10 FIN
11
12 for(@ARGV){
13 my @strings = split" ";
14 my $count = 0;
15 for my $str1(@strings){
16 /^$str1/ && /$str1$/ && ++$count for @strings
17 };
18 say"$_ -> ", $count - @strings;
19 }
Example:
./ch-2.pl "a aba ababa aa" "pa papa ma mama" "abao ab" \
"abab abab" "ab abab ababab" "abc def ghij"
Results:
a aba ababa aa -> 4
pa papa ma mama -> 2
abao ab -> 0
abab abab -> 2 *
ab abab ababab -> 3
abc def ghij -> 0
/;