finding common prefix of array of strings

后端 未结 17 1692
一向
一向 2020-11-29 06:15

I have an array like this:

$sports = array(
\'Softball - Counties\',
\'Softball - Eastern\',
\'Softball - North Harbour\',
\'Softball - South\',
\'Softball -         


        
17条回答
  •  孤城傲影
    2020-11-29 06:52

    @bumperbox

    1. Your basic code needed some correction to work in ALL scenarios!

      • Your loop only compares until one character before the last character!
      • The mismatch can possibly occur 1 loop cycle after the latest common character.
      • Hence you have to at least check until 1 character after your first string's last character.
      • Hence your comparison operator must be "<= 1" or "< 2".
    2. Currently your algorithm fails

      • if the first string is completely included in all other strings,
      • or completely included in all other strings except the last character.

    In my next answer/post, I will attach iteration optimized code!

    Original Bumperbox code PLUS correction (PHP):

    function shortest($sports) {
     $i = 1;
    
     // loop to the length of the first string
     while ($i < strlen($sports[0])) {
    
      // grab the left most part up to i in length
      // REMARK: Culturally biased towards LTR writing systems. Better say: Grab frombeginning...
      $match = substr($sports[0], 0, $i);
    
      // loop through all the values in array, and compare if they match
      foreach ($sports as $sport) {
       if ($match != substr($sport, 0, $i)) {
        // didn't match, return the part that did match
        return substr($sport, 0, $i-1);
       }
      }
     $i++; // increase string length
     }
    }
    
    function shortestCorrect($sports) {
     $i = 1;
     while ($i <= strlen($sports[0]) + 1) {
      // Grab the string from its beginning with length $i
      $match = substr($sports[0], 0, $i);
      foreach ($sports as $sport) {
       if ($match != substr($sport, 0, $i)) {
        return substr($sport, 0, $i-1);
       }
      }
      $i++;
     }
     // Special case: No mismatch happened until loop end! Thus entire str1 is common prefix!
     return $sports[0];
    }
    
    $sports1 = array(
    'Softball',
    'Softball - Eastern',
    'Softball - North Harbour');
    
    $sports2 = array(
    'Softball - Wester',
    'Softball - Western',
    );
    
    $sports3 = array(
    'Softball - Western',
    'Softball - Western',
    );
    
    $sports4 = array(
    'Softball - Westerner',
    'Softball - Western',
    );
    
    echo("Output of the original function:\n"); // Failure scenarios
    
    var_dump(shortest($sports1)); // NULL rather than the correct 'Softball'
    var_dump(shortest($sports2)); // NULL rather than the correct 'Softball - Wester'
    var_dump(shortest($sports3)); // NULL rather than the correct 'Softball - Western'
    var_dump(shortest($sports4)); // Only works if the second string is at least one character longer!
    
    echo("\nOutput of the corrected function:\n"); // All scenarios work
    var_dump(shortestCorrect($sports1));
    var_dump(shortestCorrect($sports2));
    var_dump(shortestCorrect($sports3));
    var_dump(shortestCorrect($sports4));
    

提交回复
热议问题