-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram.php
61 lines (45 loc) · 997 Bytes
/
anagram.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
function isAnagram($string1,$string2) {
if(strlen($string1) != strlen($string2) ) {
return false;
}
$count = strlen($string1);
$map1 = array();
$map2 = array();
for($i=0;$i<$count;$i++) {
if(isset($map1[$string1[$i]])) {
$map1[$string1[$i]]++;
} else {
$map1[$string1[$i]] = 1;
}
if(isset($map2[$string2[$i]])) {
$map2[$string2[$i]]++;
} else {
$map2[$string2[$i]] = 1;
}
if(isset($map2[$string1[$i]])) {
if($map1[$string1[$i]] == $map2[$string1[$i]]) {
unset($map2[$string1[$i]]);
unset($map1[$string1[$i]]);
}
}
if(isset($map1[$string2[$i]])) {
if($map1[$string2[$i]] == $map2[$string2[$i]]) {
unset($map2[$string2[$i]]);
unset($map1[$string2[$i]]);
}
}
}
if(count($map1) || count($map2)){
return false;
}
return true;
}
$string1 = "abcfg";
$string2 = "cfgah";
if(isAnagram($string1,$string2)) {
echo "$string1 $string2 are anagram\n";
} else {
echo "$string1 $string2 are not anagram\n";
}
?>