Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>@bumperbox</strong></p> <ol> <li><p>Your basic code needed some correction to work in ALL scenarios!</p> <ul> <li>Your loop only compares until one character before the last character!</li> <li>The mismatch can possibly occur 1 loop cycle after the latest common character.</li> <li>Hence you have to at least check until 1 character after your first string's last character.</li> <li>Hence your comparison operator must be "&lt;= 1" or "&lt; 2".</li> </ul></li> <li><p>Currently your algorithm fails</p> <ul> <li>if the first string is completely included in all other strings,</li> <li>or completely included in all other strings except the last character.</li> </ul></li> </ol> <p>In my next answer/post, I will attach iteration optimized code!</p> <p><strong>Original Bumperbox code PLUS correction (PHP):</strong></p> <pre><code>function shortest($sports) { $i = 1; // loop to the length of the first string while ($i &lt; 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 &lt;= 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)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload