PHP compares two strings at random position
PHP to compare two strings
Example. I got a line with a number
1 2 2 1 and another is 2 1 2 1
The result will be true since its just a position shuffle for 1 2 2 1 and 2 2 1 1
But if the value
1 2 2 2 and another is 2 1 2 1
the result will return false because on the first line it got 3 occurrences of 2, no matter how we shuffle the position 1 2 2 2 it will not give 2 1 2 1
another possible example would be
1 2 3 1 and another is 1 2 3 3
the result will be false and because no matter how we shuffle 1231, we will not give us 1233
How can I make this comparison, is there any php function that can compare a shuffle string with an unmixed character
Maybe I could use str_shuffle
and make the array push to unique until I get 24 combinations of that string.
$input_string = "1221";
$array_string = ();
while( count($array_string) != 24 )
{
$input_string = str_shuffle($input_string);
array_push($array_string, $input_string);
$array_string = array_unique($array_string);
}
//then lastly i check if compare string is in array
Is this the correct way to do it?
Edit: Another suggestion, which seems to be better, splits the string into an array and sorts it. which i think will save more processing power!
source to share
You can compare the contents of 2 lists, regardless of order, very efficiently by first sorting both lists, then comparing them. If both sorted versions are identical, then it is possible to shuffle one line to another.
It's actually called anagram (and it can also be done with letters).
If the digits are the only digit, you can sort the characters in the string. If the letters have multiple digits, then you must first build an array of integers and sort them.
How much time will it take
This method will take time proportional to length * log(length)
(where length
is the length of the strings) as it takes the sorting. This algorithm has a time complexity of O (n lg (n)) .
source to share
You want to check if a numeric string is an anagram of another. You can match each number to a prime number, take the product of the displayed numbers, and then perform an equality. The same idea can be used to make it work for alphanumeric strings. This will not work for long strings, as it will cause an integer overflow. If you expect long strings, a counting approach is better. Unlike splitting, sorting, and comparing two arrays, this is done in O (n).
$primeMap = array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
function isNumericAnagram ($n1, $n2) {
if(strlen($n1) !== strlen($n2)) return false;
$p1 = $p2 = 1;
for($i = 0; $i < strlen($n1); $i ++) {
$p1 *= $primeMap[$n1[$i]];
$p2 *= $primeMap[$n2[$i]];
}
return $p1 === $p2;
}
isNumericAnagram ('1234', '3214'); //true
isNumericAnagram ('12534', '35214'); //true
isNumericAnagram ('0000', '0000'); //true
isNumericAnagram ('11', '1'); //false
From the main theorem of arithmetic,
any integer greater than 1 can be written as a single product (before ordering the factors) of primes
source to share
The assumptions I'm making here are that your strings only contain numbers and spaces, and the numbers are separated by spaces (i.e. all integers). With these assumptions in mind and assuming that you are looking for the most efficient way to solve this problem, my solution would be as follows.
/**
*
* A function that returns TRUE or FALSE if both strings
* are an anagram. Assumed space-delimited integer strings.
*
*/
function identStrings($string1, $string2) {
$token = strtok($string1, " "); // tokenize the string by spaces
$numbers = array_map('intval',array_filter(explode(' ', $string2)));
while($token !== false) {
if (!$token)
continue;
if (($key = array_search((int)$token, $numbers)) === false)
return false;
else
unset($numbers[$key]);
$token = strtok(" ");
}
return true;
}
$string1 = "1 2 2 2";
$string2 = "2 1 2 1";
if (identStrings($string1, $string2)) {
echo "These strings are identical!";
} else {
echo "These strings are not identical...";
}
source to share
just another solution using some logic:
$str1 = "1 2 3 1";
$str2 = "1 1 3 2";
function sameStrings($str1, $str2)
{
//check for the string lengths first
if(strlen($str1) != strlen($str2))
return false;
$strArr = explode(' ', $str1);
$occArr = array();
//Get occurances of all numbers in the first string
foreach($strArr as $str) $occArr[$str] = $occArr[$str]+1;
//compare them with the second string, if the length of an of those numbers is not corrct then its false
foreach($occArr as $num => $occ)
{
if(substr_count($str2, $num) != $occ)
return false;
}
return true;
}
echo sameStrings($str1, $str2);
source to share