Jump to content

string compare- can u improve my code?


AP81

Recommended Posts

Hi,

 

I needed to compare strings to see if they contain all the same characters, but aren't identical.  For example, comparing 2345 and 3245.  These strings aren't identical, but contain the same characters. 

 

The reason why I needed to do this was to cross check some numbers from two different databases (a couple of previous employees were dyslexic).  I am porting over all this information from one DB to another, thus I am cleaning the data to make sure it is accurate.

 

Anyway, I've written my own solution to this (see strings_contain_same_chars function below) as I couldn't find a PHP function which does this.  Although it works fine, there is a fair amount of overhead by creating arrays to store the data, then sort, then implode.  I was wondering if there is a neater way to do this.

 

<?php
$string1 = '2345';
$string2 = '3245';

function strings_contain_same_chars($string1, $string2)
{
$lenStr1 = strlen($string1);
$lenStr2 = strlen($string2);
if ($lenStr1 != $lenStr2) {  // invalid if not the same length
	return false;
}	else {
	for ($i=0; $i<$lenStr1; $i++) {
		$str1[$i] = substr($string1, $i, 1);
	}
	for ($i=0; $i<$lenStr2; $i++) {
		$str2[$i] = substr($string2, $i, 1);
	}
	sort($str1);
	sort($str2);

	$str1 = implode('', $str1);
	$str2 = implode('', $str2);

	if ($str1 == $str2) {
		return true;
	}
	return false;
}
}
?>

 

Link to comment
Share on other sites

Well, you could have made your code a lot shorted by converting the strings to arrays, sorting and then checking for equality. Something like:

 

<?php
$string1 = 'abc';
$string2 = 'acb';

function strings_contain_same_chars($string1, $string2)
{
    $array1 = str_split($string1);
    $array2 = str_split($string2);
    sort($array1);
    sort($array2);
    if($array1 == $array2){
        return true;
    }else{
        return false;
    }
}
if(strings_contain_same_chars($string1,$string2)){
    echo 'true';
}else{
    echo 'false';
}
?>

 

Wether or not it's any more efficient, im not sure.

Link to comment
Share on other sites

Did some benchmarking -- my method is about twice as quick:

 

<?php
$string1 = 'abcdefghi';
$string2 = 'dbceghifa';

function function1($string1, $string2)
{
    $array1 = str_split($string1);
    $array2 = str_split($string2);
    sort($array1);
    sort($array2);
    if($array1 == $array2){
        return true;
    }else{
        return false;
    }
}
function function2($string1, $string2)
{
$lenStr1 = strlen($string1);
$lenStr2 = strlen($string2);
if ($lenStr1 != $lenStr2) {  // invalid if not the same length
	return false;
}	else {
	for ($i=0; $i<$lenStr1; $i++) {
		$str1[$i] = substr($string1, $i, 1);
	}
	for ($i=0; $i<$lenStr2; $i++) {
		$str2[$i] = substr($string2, $i, 1);
	}
	sort($str1);
	sort($str2);

	$str1 = implode('', $str1);
	$str2 = implode('', $str2);

	if ($str1 == $str2) {
		return true;
	}
	return false;
}
}

$t1 = microtime(true);
for($x=0;$x<=10000;$x++){
    function1($string1,$string2);
}
$t2 = microtime(true);
for($x=0;$x<=10000;$x++){
    function2($string1,$string2);
}
$t3 = microtime(true);//end time

//now output results
printf ("1st Method: %0.8f<br>2nd Method: %0.8f<br>Ratio (1st method/2nd method): %0.2f", $t2-$t1, $t3-$t2, ($t2-$t1)/($t3-$t2));
?>

Link to comment
Share on other sites

Or even quicker:

 

function function2($string1, $string2)
{
    $array1 = str_split($string1);
    $array2 = str_split($string2);
    $count1 = count($array1);
    $count2 = count($array2);
    if(count(array_intersect($array1,$array2))==$count1 &&$count1==$count2){
        return true;
    }else{
        return false;
    }
}

 

Interestingly, the speed difference is more noticeable with a set of numbers than a set of letters. Not sure why that should be, though.

 

<?php
$string1 = 'abcdefghi';
$string2 = 'ihgfedcba';
$string1 = '1234567';
$string2 = '7654321';

function function1($string1, $string2)
{
    $array1 = str_split($string1);
    $array2 = str_split($string2);
    sort($array1);
    sort($array2);
    if($array1 == $array2){
        return true;
    }else{
        return false;
    }
}
function function2($string1, $string2)
{
    $array1 = str_split($string1);
    $array2 = str_split($string2);
    $count1 = count($array1);
    $count2 = count($array2);
    if(count(array_intersect($array1,$array2))==$count1 &&$count1==$count2){
        return true;
    }else{
        return false;
    }
}
$t1 = microtime(true);
for($x=0;$x<=10000;$x++){
    function1($string1,$string2);
}
$t2 = microtime(true);
for($x=0;$x<=10000;$x++){
    function2($string1,$string2);
}
$t3 = microtime(true);//end time

//now output results
printf ("1st Method: %0.8f<br>2nd Method: %0.8f<br>Ratio (1st method/2nd method): %0.2f", $t2-$t1, $t3-$t2, ($t2-$t1)/($t3-$t2));
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.