/
ch-2.pl
49 lines (39 loc) · 1.18 KB
/
ch-2.pl
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl
use warnings;
use strict;
# Task 2: Consistent Strings
# You are given an array of strings and allowed string having distinct characters.
# A string is consistent if all characters in the string appear in the string allowed.
# Example 1
my @str = ("ad", "bd", "aaab", "baa", "badab");
my $allowed = "ab";
check_allowed($allowed, \@str);
# Example 2
@str = ("a", "b", "c", "ab", "ac", "bc", "abc");
$allowed = "abc";
check_allowed($allowed, \@str);
# Example 3
@str = ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d");
$allowed = "cad";
check_allowed($allowed, \@str);
sub check_allowed {
my ($allowed, $str_ref) = @_;
my $success_count = 0;
# Loop through each item in array of strings
foreach my $str (@$str_ref) {
# Loop through each character in string to check in allowed list
my @chars = (split('', $str));
my $length = scalar @chars;
for (my $i = 0; $i < $length; $i++) {
my $char = $chars[$i];
# if character is allowed keep checking or increment success if it's the last in the string
if (index($allowed, $char) != -1) {
$success_count++ if $i == $length - 1;
}
else {
last;
}
}
}
print "$success_count\n";
}