php - Optimize substrings anagram compare algorithm -


im trying solve 1 challenge have check string substrings anagrams. condition s=abba, anagramic pairs are: {s[1,1],s[4,4]}, {s[1,2],s[3,4]}, {s[2,2],s[3,3]} , {s[1,3],s[2,4]}

problem have string 100 chars , execution time should below 9 secs. time around 50 secs... below code, appreciate advice - if give me directions or pseudo code better.

$time1 = microtime(true); $string = 'abdcasdabvdvafsgfdsvafdsafewsrgsdcasfsdfgxccafdsgccafsdgsdcascdsfsdfsdgfadasdgsdfawdascsdsasdasgsdfs'; $arr = []; $len = strlen($string); ($i = 0; $i < strlen($string); $i++) {     if ($i === 0) {         ($j = 1; $j <= $len - 1; $j++) {             $push = substr($string, $i, $j);             array_push($arr, $push);         }     } else {         ($j = 1; $j <= $len - $i; $j++) {             $push = substr($string, $i, $j);             array_push($arr, $push);         }     } } $br = 0; $arrlength = count($arr); foreach ($arr $key => $val) {     if ($key === count($arr) - 1) {         break;     }     ($k = $key + 1; $k < $arrlength; $k++) {         if (is_anagram($val, $arr[$k]) === true) {             $br++;         }     } }     echo $br."</br>";   function is_anagram($a, $b) {     $result = (count_chars($a, 1) == count_chars($b, 1));     return $result; } $time2 = microtime(true); echo "script execution time: ".($time2-$time1); 

edit:

hi again, today had time tried optimize couldnt crack this... new code think got worse. advanced suggestions ?

<?php  $string = 'abdcasdabvdvafsgfdsvafdsafewsrgsdcasfsdfgxccafdsgccafsdgsdcascdsfsdfsdgfadasdgsdfawdascsdsasdasgsdfs';     $arr = []; $len = strlen($string); ($i = 0; $i < strlen($string); $i++) {     if ($i === 0) {         ($j = 1; $j <= $len - 1; $j++) {              $push = substr($string, $i, $j);             array_push($arr, $push);         }     } else {         ($j = 1; $j <= $len - $i; $j++) {             $push = substr($string, $i, $j);             array_push($arr, $push);         }     } }  $br = 0; $arrlen = count ($arr); foreach ($arr $key => $val) {     if (($key === $arrlen - 1)) {         break;     }      ($k = $key + 1; $k < $arrlen; $k++) {      $result = stringscompare($val,$arr[$k]);         if ($result === true)         {             $br++;         }  }      echo $br."\n"; }  function stringscompare($a,$b) {     $lenone = strlen($a);     $lentwo = strlen ($b);     if ($lenone !== $lentwo)     {         return false;     }     else {         $fail = 0;         if ($lenone === 1) {             if ($a === $b) {                 return true;             }             else             {                 return false;             }         }         else         {         ($x = 0; $x < $lenone; $x++)         {          $position = strpos($b,$a[$x]);              if($position === false)              {                  $fail = 1;                  break;               }             else             {                 $b[$position] = 0;                 $fail = 0;             }         }         if ($fail === 1)         {             return false;         }             else             {                 return true;             }     }         } } ?> 

you should think of rule anagrams of string can meet. example, number of occurrences of each character.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -