The weekly challenge 255 - Task 1: Odd Character
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-255/#TASK1 3 # 4 # Task 1: Odd Character 5 # ===================== 6 # 7 # You are given two strings, $s and $t. The string $t is generated using the 8 # shuffled characters of the string $s with an additional character. 9 # 10 # Write a script to find the additional character in the string $t.. 11 # 12 ## Example 1 13 ## 14 ## Input: $s = "Perl" $t = "Preel" 15 ## Output: "e" 16 # 17 ## Example 2 18 ## 19 ## Input: $s = "Weekly" $t = "Weeakly" 20 ## Output: "a" 21 # 22 ## Example 3 23 ## 24 ## Input: $s = "Box" $t = "Boxy" 25 ## Output: "y" 26 # 27 ############################################################ 28 ## 29 ## discussion 30 ## 31 ############################################################ 32 # 33 # Split the word into its character, store them in a hash table, then 34 # count the result for each character in both the table for the original 35 # word and for the new word. 36 37 use strict; 38 use warnings; 39 40 odd_character("Perl", "Preel"); 41 odd_character("Weekly", "Weeakly"); 42 odd_character("Box", "Boxy"); 43 44 sub odd_character { 45 my ($s, $t) = @_; 46 print "Input: '$s', '$t'\n"; 47 my $s_hash = {}; 48 my $t_hash = {}; 49 foreach my $char (split//,$s) { 50 $s_hash->{$char}++; 51 } 52 foreach my $char (split//,$t) { 53 $t_hash->{$char}++; 54 } 55 foreach my $found (keys %$t_hash) { 56 $s_hash->{$found} //= 0; 57 if($t_hash->{$found} > $s_hash->{$found}) { 58 print "Output: $found\n"; 59 return; 60 } 61 } 62 }