aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-07-09 22:26:46 +0100
committerGitHub <noreply@github.com>2023-07-09 22:26:46 +0100
commitad44b0229b7c4fda533536e8f357de3b3cf584e7 (patch)
tree305e657180d4fb60618b60914a3445573be0b363
parent6fdcae0fc9efa65c474e88b60a2bf87992f4208f (diff)
parent129e41b0469fc834b40d1008486c593d914f4768 (diff)
downloadperlweeklychallenge-club-ad44b0229b7c4fda533536e8f357de3b3cf584e7.tar.gz
perlweeklychallenge-club-ad44b0229b7c4fda533536e8f357de3b3cf584e7.tar.bz2
perlweeklychallenge-club-ad44b0229b7c4fda533536e8f357de3b3cf584e7.zip
Merge pull request #8343 from Solathian/branch-for-challenge-224
Added file!
-rw-r--r--challenge-224/solathian/perl/ch-1.pl49
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