blob: 3094dd81657b74e3822b9fe3c290c58aaadb2e3e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
use strict;
use warnings;
use v5.38;
sub twice_first( $str ) {
my %counts;
grep {
# return if the count is already 1
return $_ if $counts{ $_ };
# increment the count
++$counts{ $_ }
} ( split '', $str );
# if there are no doubled letters return empty string
return '';
}
my @inputs = (
"acbddbca",
"abccd",
"abcdabbb",
"abcdefg"
);
for (@inputs) {
say $_ . " => " . twice_first($_);
}
|