-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.php
45 lines (40 loc) · 953 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
<?php
/**
* Verify is the given strings form anagrams of each other
*
* @param $string1
* @param $string2
* @return bool
*/
function isAnagram($string1, $string2)
{
if (invalidInputs($string1, $string2)) {
return false;
}
return (count_chars(sanatizeString($string1), 1) == count_chars(sanatizeString($string2), 1));
}
/**
* Verify is the strings are valid
*
* @param $string1
* @param $string2
* @return bool
*/
function invalidInputs($string1, $string2)
{
return (!is_string($string1) || !is_string($string2));
}
/**
* Sanatize the given string
*
* @param $string
* @return mixed
*/
function sanatizeString($string)
{
return str_replace(" ", "", strtolower($string));
}
var_dump(isAnagram('admirer', 'married'));
var_dump(isAnagram('AstroNomers', 'no more stars'));
var_dump(isAnagram(1, 'no more stars'));
var_dump(isAnagram('random url', 'random urt'));