@@ -10,31 +10,31 @@ discard block |
||
| 10 | 10 | class ShortestCommonSuperstring |
| 11 | 11 | { |
| 12 | 12 | /** |
| 13 | - * @var integer Affix length for current iteration |
|
| 14 | - */ |
|
| 13 | + * @var integer Affix length for current iteration |
|
| 14 | + */ |
|
| 15 | 15 | protected $len; |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * @var string[] Prefixes of current length |
|
| 19 | - */ |
|
| 18 | + * @var string[] Prefixes of current length |
|
| 19 | + */ |
|
| 20 | 20 | protected $prefixes; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * @var string[] Strings being merged |
|
| 24 | - */ |
|
| 23 | + * @var string[] Strings being merged |
|
| 24 | + */ |
|
| 25 | 25 | protected $strings; |
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | - * @var string[] Suffixes of current length |
|
| 29 | - */ |
|
| 28 | + * @var string[] Suffixes of current length |
|
| 29 | + */ |
|
| 30 | 30 | protected $suffixes; |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | - * Get the shortest string that contains all given strings |
|
| 34 | - * |
|
| 35 | - * @param string[] $strings |
|
| 36 | - * @return string |
|
| 37 | - */ |
|
| 33 | + * Get the shortest string that contains all given strings |
|
| 34 | + * |
|
| 35 | + * @param string[] $strings |
|
| 36 | + * @return string |
|
| 37 | + */ |
|
| 38 | 38 | public function getShortest(array $strings): string |
| 39 | 39 | { |
| 40 | 40 | $this->strings = array_unique($strings); |
@@ -56,8 +56,8 @@ discard block |
||
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | - * Compare given strings |
|
| 60 | - */ |
|
| 59 | + * Compare given strings |
|
| 60 | + */ |
|
| 61 | 61 | protected static function compareStrings(string $a, string $b): int |
| 62 | 62 | { |
| 63 | 63 | $aLen = strlen($a); |
@@ -73,21 +73,21 @@ discard block |
||
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | - * Return the list of keys pointing to strings whose prefix is identical to their suffix |
|
| 77 | - * |
|
| 78 | - * @return integer[] |
|
| 79 | - */ |
|
| 76 | + * Return the list of keys pointing to strings whose prefix is identical to their suffix |
|
| 77 | + * |
|
| 78 | + * @return integer[] |
|
| 79 | + */ |
|
| 80 | 80 | protected function getIdenticalAffixKeys(): array |
| 81 | 81 | { |
| 82 | 82 | return array_keys(array_intersect_assoc($this->prefixes, $this->suffixes)); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
| 86 | - * Match and merge a string by key |
|
| 87 | - * |
|
| 88 | - * @param integer $leftKey Left string's key |
|
| 89 | - * @return bool Whether a match was found and strings merged |
|
| 90 | - */ |
|
| 86 | + * Match and merge a string by key |
|
| 87 | + * |
|
| 88 | + * @param integer $leftKey Left string's key |
|
| 89 | + * @return bool Whether a match was found and strings merged |
|
| 90 | + */ |
|
| 91 | 91 | protected function mergeString(int $leftKey): bool |
| 92 | 92 | { |
| 93 | 93 | // Temporarily blank this string's prefix from the array to avoid matches |
@@ -109,8 +109,8 @@ discard block |
||
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
| 112 | - * Merge two stored strings together at current affix length |
|
| 113 | - */ |
|
| 112 | + * Merge two stored strings together at current affix length |
|
| 113 | + */ |
|
| 114 | 114 | protected function mergeStringPair(int $leftKey, int $rightKey): void |
| 115 | 115 | { |
| 116 | 116 | $this->strings[$leftKey] .= substr($this->strings[$rightKey], $this->len); |
@@ -119,8 +119,8 @@ discard block |
||
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | /** |
| 122 | - * Merge all stored strings using current affix length |
|
| 123 | - */ |
|
| 122 | + * Merge all stored strings using current affix length |
|
| 123 | + */ |
|
| 124 | 124 | protected function mergeStrings(): void |
| 125 | 125 | { |
| 126 | 126 | $this->storeAffixes(); |
@@ -137,11 +137,11 @@ discard block |
||
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | /** |
| 140 | - * Match and merge strings from given group |
|
| 141 | - * |
|
| 142 | - * @param integer[] $keys List of keys |
|
| 143 | - * @return void |
|
| 144 | - */ |
|
| 140 | + * Match and merge strings from given group |
|
| 141 | + * |
|
| 142 | + * @param integer[] $keys List of keys |
|
| 143 | + * @return void |
|
| 144 | + */ |
|
| 145 | 145 | protected function mergeStringsGroup(array $keys): void |
| 146 | 146 | { |
| 147 | 147 | foreach ($keys as $leftKey) |
@@ -157,8 +157,8 @@ discard block |
||
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
| 160 | - * Remove empty strings from the list |
|
| 161 | - */ |
|
| 160 | + * Remove empty strings from the list |
|
| 161 | + */ |
|
| 162 | 162 | protected function removeEmptyStrings(): void |
| 163 | 163 | { |
| 164 | 164 | if (end($this->strings) === '') |
@@ -168,8 +168,8 @@ discard block |
||
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
| 171 | - * Remove fully-overlapping strings from the list |
|
| 172 | - */ |
|
| 171 | + * Remove fully-overlapping strings from the list |
|
| 172 | + */ |
|
| 173 | 173 | protected function removeFullyOverlappingStrings(): void |
| 174 | 174 | { |
| 175 | 175 | $strlen = array_map('strlen', $this->strings); |
@@ -196,8 +196,8 @@ discard block |
||
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
| 199 | - * Reset the keys in the string list to remove the gaps |
|
| 200 | - */ |
|
| 199 | + * Reset the keys in the string list to remove the gaps |
|
| 200 | + */ |
|
| 201 | 201 | protected function resetKeys(): void |
| 202 | 202 | { |
| 203 | 203 | end($this->strings); |
@@ -208,18 +208,18 @@ discard block |
||
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
| 211 | - * Sort the stored strings |
|
| 212 | - */ |
|
| 211 | + * Sort the stored strings |
|
| 212 | + */ |
|
| 213 | 213 | protected function sortStrings(): void |
| 214 | 214 | { |
| 215 | 215 | usort($this->strings, [__CLASS__, 'compareStrings']); |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
| 219 | - * Capture and stored affixes of current length |
|
| 220 | - * |
|
| 221 | - * Will only store affixes from strings that are longer than current affix length |
|
| 222 | - */ |
|
| 219 | + * Capture and stored affixes of current length |
|
| 220 | + * |
|
| 221 | + * Will only store affixes from strings that are longer than current affix length |
|
| 222 | + */ |
|
| 223 | 223 | protected function storeAffixes(): void |
| 224 | 224 | { |
| 225 | 225 | $this->prefixes = []; |