Skip to content

Commit

Permalink
Use array_search() to search for matching affixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Nov 3, 2019
1 parent f6dcfcf commit 1975726
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
}
},
"extra": {
"version": "2.0.2"
"version": "2.0.3"
}
}
22 changes: 13 additions & 9 deletions src/ShortestCommonSuperstring.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,22 @@ protected function getIdenticalAffixKeys(): array
*/
protected function mergeString(int $leftKey): bool
{
$suffix = $this->suffixes[$leftKey];
foreach ($this->prefixes as $rightKey => $prefix)
{
if ($prefix === $suffix && $leftKey !== $rightKey)
{
$this->mergeStringPair($leftKey, $rightKey);
// Temporarily blank this string's prefix from the array to avoid matches
$prefix = $this->prefixes[$leftKey];
$this->prefixes[$leftKey] = '';

return true;
}
// Search for a prefix that matches this string's suffix before restoring its prefix
$rightKey = array_search($this->suffixes[$leftKey], $this->prefixes, true);
$this->prefixes[$leftKey] = $prefix;

if ($rightKey === false)
{
return false;
}

return false;
$this->mergeStringPair($leftKey, $rightKey);

return true;
}

/**
Expand Down

0 comments on commit 1975726

Please sign in to comment.