diff options
| -rw-r--r-- | challenge-224/solathian/perl/ch-1.pl | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-224/solathian/perl/ch-1.pl b/challenge-224/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..aefb3c2935 --- /dev/null +++ b/challenge-224/solathian/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!usr/bin/perl +use v5.36; + +# Challenge 224 - 1 - Special Notes + +# You are given two strings, $source and $target. +# Write a script to find out if using the characters (only once) from source, a target string can be created. + +sn("abc", "xyz"); # false +sn("scriptinglanguage", "perl"); # true +sn("aabbcc", "abc"); # true + + +sub sn($source, $target) +{ + my $retVal = "true"; + + # create arrays from the strings + my @source = split(//, $source); + my @target = split(//, $target); + + + # build hash + my %source; + foreach my $elem (@source) + { + $source{$elem}++; + } + + # check the target against the hash, decrase counter if needed, remove elements if 0 is reached + foreach my $targetChar (@target) + { + if(exists $source{$targetChar}) + { + $source{$targetChar}--; + + delete $source{$targetChar} if($source{$targetChar} == 0) + + } + else + { + $retVal = "false"; + last; + } + } + + say $retVal; + +}
\ No newline at end of file |
