Jump to content

Stringy thingies.


0perator

Recommended Posts

<?php

function is_scrambled ($scrambled, $unscrambled)
{
if(strlen($scrambled) != strlen($unscrambled))
	return false;

$letters = str_split($scrambled);
$num_letters = count($letters);
sort($letters);
for($i = 0; $i < $num_letters; $i++)
	if($letters[$i] != $unscrambled[$i])
		return false;
return true;
}

?>

 

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568422
Share on other sites

Here's another way:

<?php
$value_sc = "qewryt";
$value_unsc = "qwerty";
$sc_arr = str_split($value_sc);
$unsc_arr = str_split($value_unsc);
sort($sc_arr);
sort($unsc_arr);
$diff = array_diff($sc_arr,$unsc_arr);
if (empty($diff)) echo 'The two strings have the same characters';
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568428
Share on other sites

I would expect Orio's to be a tiny bit faster, because when you compare an array, how exactly does PHP go about doing that?  Does it check each element?  Also, you have to sort with the second one.

 

Also, Orio's would probably scale better, since operating on arrays logically becomes slower as the array gets bigger (and the rate at which a loop slows, I would assume would be slower).

 

Anyway, just a guess.

Link to comment
https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568483
Share on other sites

Oh you're right....  Didn't see that....  I didn't read his entire code, just looked at the main difference between the two.

 

The only way that Orio's would work would be if the unscrambled string was already sorted....

 

Guess Orio needs another str_split and sort after all x.x.

Link to comment
https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568495
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.