File tree Expand file tree Collapse file tree 1 file changed +81
-0
lines changed Expand file tree Collapse file tree 1 file changed +81
-0
lines changed Original file line number Diff line number Diff line change
1
+ # !/usr/bin/perl -w
2
+ use Data::Dumper;
3
+ my %prime_map = (
4
+ ' a' => 2,
5
+ ' b' => 3,
6
+ ' c' => 5,
7
+ ' d' => 7,
8
+ ' e' => 11,
9
+ ' f' => 13,
10
+ ' g' => 17,
11
+ ' h' => 19,
12
+ ' i' => 23,
13
+ ' j' => 29,
14
+ ' k' => 31,
15
+ ' l' => 37,
16
+ ' m' => 41,
17
+ ' n' => 43,
18
+ ' o' => 47,
19
+ ' p' => 53,
20
+ ' q' => 59,
21
+ ' r' => 61,
22
+ ' s' => 67,
23
+ ' t' => 71,
24
+ ' u' => 73,
25
+ ' v' => 79,
26
+ ' w' => 83,
27
+ ' x' => 89,
28
+ ' y' => 97,
29
+ ' z' => 101,
30
+ ' A' => 103,
31
+ ' B' => 107,
32
+ ' C' => 109,
33
+ ' D' => 113,
34
+ ' E' => 127,
35
+ ' F' => 131,
36
+ ' G' => 137,
37
+ ' H' => 139,
38
+ ' I' => 149,
39
+ ' J' => 151,
40
+ ' K' => 163,
41
+ ' L' => 167,
42
+ ' M' => 173,
43
+ ' N' => 179,
44
+ ' O' => 181,
45
+ ' P' => 191,
46
+ ' Q' => 193,
47
+ ' R' => 197,
48
+ ' S' => 199,
49
+ ' T' => 211,
50
+ ' U' => 223,
51
+ ' V' => 227,
52
+ ' W' => 229,
53
+ ' X' => 233,
54
+ ' Y' => 239,
55
+ ' Z' => 241
56
+ );
57
+ sub compute_checkproduct {
58
+ (my $input_string ) = @_ ;
59
+ my @characters = split ' ' , $input_string ;
60
+ my $checkproduct = 1;
61
+ foreach $character (@characters ) {
62
+ $checkproduct *= $prime_map {$character };
63
+ }
64
+ $checkproduct ;
65
+ }
66
+ sub detect_anagrams {
67
+ (my $haystack , my $needle ) = @_ ;
68
+ my $found = 0, $needle_length = length ($needle ), $haystack_length = length ($haystack );
69
+ return 0 if ($haystack_length < $needle_length );
70
+ my $needle_checkproduct = compute_checkproduct( $needle );
71
+ for ( $i = 0; $i < (length ($haystack ) - length ($needle )); $i ++ ) {
72
+ my $haystack_slice = substr ( $haystack , $i , length ($needle ) );
73
+ my $haystack_slice_checkproduct = compute_checkproduct( $haystack_slice );
74
+ $found ++ if ( $haystack_slice_checkproduct == $needle_checkproduct );
75
+ }
76
+ $found ;
77
+ }
78
+ print detect_anagrams(' AdnBndAndBdaBn' , ' dAn' );
79
+ print " \n " ;
80
+ print detect_anagrams(' AbrAcadAbRa' , ' cAda' );
81
+ print " \n " ;
You can’t perform that action at this time.
0 commit comments