diff options
| author | Solathian <horvath6@gmail.com> | 2023-07-09 23:24:22 +0200 |
|---|---|---|
| committer | Solathian <horvath6@gmail.com> | 2023-07-09 23:24:22 +0200 |
| commit | 129e41b0469fc834b40d1008486c593d914f4768 (patch) | |
| tree | 490e35ee68dcbb16f025b0affee0b79594b11246 | |
| parent | bb56db19a6d825b862aecbd16012f52eb017fc4f (diff) | |
| download | perlweeklychallenge-club-129e41b0469fc834b40d1008486c593d914f4768.tar.gz perlweeklychallenge-club-129e41b0469fc834b40d1008486c593d914f4768.tar.bz2 perlweeklychallenge-club-129e41b0469fc834b40d1008486c593d914f4768.zip | |
Added file!
| -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 |
