diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-27 16:06:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-27 16:06:34 +0000 |
| commit | 52614c1e8a3c9e56a894e8f4fd0916b25865883d (patch) | |
| tree | 2d54b01ac3e4f0d6b145f72b0e7658ab58f25edf /challenge-092 | |
| parent | 792fa135becdb0a534969af549cea5a846ee9fa1 (diff) | |
| parent | 59bb5a96799bf1461e105ecba08e5b573be35989 (diff) | |
| download | perlweeklychallenge-club-52614c1e8a3c9e56a894e8f4fd0916b25865883d.tar.gz perlweeklychallenge-club-52614c1e8a3c9e56a894e8f4fd0916b25865883d.tar.bz2 perlweeklychallenge-club-52614c1e8a3c9e56a894e8f4fd0916b25865883d.zip | |
Merge pull request #3080 from Cris-HD/branch-for-challenge-092
Added challenge 92 solution
Diffstat (limited to 'challenge-092')
| -rw-r--r-- | challenge-092/cristian-heredia/perl/ch-1.pl | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/challenge-092/cristian-heredia/perl/ch-1.pl b/challenge-092/cristian-heredia/perl/ch-1.pl new file mode 100644 index 0000000000..234aed7a1a --- /dev/null +++ b/challenge-092/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,73 @@ +=begin + + TASK #1 › Isomorphic Strings + Submitted by: Mohammad S Anwar + You are given two strings $A and $B. + + Write a script to check if the given strings are Isomorphic. Print 1 if they are otherwise 0. + + Example 1: + Input: $A = "abc"; $B = "xyz" + Output: 1 + Example 2: + Input: $A = "abb"; $B = "xyy" + Output: 1 + Example 3: + Input: $A = "sum"; $B = "add" + Output: 0 +=end +=cut + + +use strict; +use warnings; +use Data::Dumper; + +my $A = "abc"; +my $B = "xyz"; +my %hash; +my @sets; +my $C1; +my $C2; + +#Convert into arrays +my @arrayA = split(//, $A); +my @arrayB = split(//, $B); + +foreach (my $i=0; $i<@arrayA; $i++) { + $C1 = $arrayA[$i]; + $C2 = $arrayB[$i]; + checkHash(); +} + +print "Output: 1\n"; + +sub addIntoHash { + $hash{$C1} = $C2; +} + +sub checkHash { + if(!exists $hash{$C1}) { + if(grep $_ eq $C2, @sets) { + print "Output: 0\n"; + exit; + } + else { + push @sets, $C2; + addIntoHash(); + } + } + elsif ($hash{$C1} eq $C2) { + addIntoHash(); + } + else { + print "Output: 0\n"; + exit; + } +} + + + + + + |
