@@ -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 | $suffix = $this->suffixes[$leftKey]; |
@@ -105,8 +105,8 @@ discard block |
||
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
| 108 | - * Merge two stored strings together at current affix length |
|
| 109 | - */ |
|
| 108 | + * Merge two stored strings together at current affix length |
|
| 109 | + */ |
|
| 110 | 110 | protected function mergeStringPair(int $leftKey, int $rightKey): void |
| 111 | 111 | { |
| 112 | 112 | $this->strings[$leftKey] .= substr($this->strings[$rightKey], $this->len); |
@@ -115,8 +115,8 @@ discard block |
||
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /** |
| 118 | - * Merge all stored strings using current affix length |
|
| 119 | - */ |
|
| 118 | + * Merge all stored strings using current affix length |
|
| 119 | + */ |
|
| 120 | 120 | protected function mergeStrings(): void |
| 121 | 121 | { |
| 122 | 122 | $this->storeAffixes(); |
@@ -133,11 +133,11 @@ discard block |
||
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | /** |
| 136 | - * Match and merge strings from given group |
|
| 137 | - * |
|
| 138 | - * @param integer[] $keys List of keys |
|
| 139 | - * @return void |
|
| 140 | - */ |
|
| 136 | + * Match and merge strings from given group |
|
| 137 | + * |
|
| 138 | + * @param integer[] $keys List of keys |
|
| 139 | + * @return void |
|
| 140 | + */ |
|
| 141 | 141 | protected function mergeStringsGroup(array $keys): void |
| 142 | 142 | { |
| 143 | 143 | foreach ($keys as $leftKey) |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
| 156 | - * Remove empty strings from the list |
|
| 157 | - */ |
|
| 156 | + * Remove empty strings from the list |
|
| 157 | + */ |
|
| 158 | 158 | protected function removeEmptyStrings(): void |
| 159 | 159 | { |
| 160 | 160 | if (end($this->strings) === '') |
@@ -164,8 +164,8 @@ discard block |
||
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /** |
| 167 | - * Remove fully-overlapping strings from the list |
|
| 168 | - */ |
|
| 167 | + * Remove fully-overlapping strings from the list |
|
| 168 | + */ |
|
| 169 | 169 | protected function removeFullyOverlappingStrings(): void |
| 170 | 170 | { |
| 171 | 171 | $strlen = array_map('strlen', $this->strings); |
@@ -192,8 +192,8 @@ discard block |
||
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
| 195 | - * Reset the keys in the string list to remove the gaps |
|
| 196 | - */ |
|
| 195 | + * Reset the keys in the string list to remove the gaps |
|
| 196 | + */ |
|
| 197 | 197 | protected function resetKeys(): void |
| 198 | 198 | { |
| 199 | 199 | end($this->strings); |
@@ -204,18 +204,18 @@ discard block |
||
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /** |
| 207 | - * Sort the stored strings |
|
| 208 | - */ |
|
| 207 | + * Sort the stored strings |
|
| 208 | + */ |
|
| 209 | 209 | protected function sortStrings(): void |
| 210 | 210 | { |
| 211 | 211 | usort($this->strings, [__CLASS__, 'compareStrings']); |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | /** |
| 215 | - * Capture and stored affixes of current length |
|
| 216 | - * |
|
| 217 | - * Will only store affixes from strings that are longer than current affix length |
|
| 218 | - */ |
|
| 215 | + * Capture and stored affixes of current length |
|
| 216 | + * |
|
| 217 | + * Will only store affixes from strings that are longer than current affix length |
|
| 218 | + */ |
|
| 219 | 219 | protected function storeAffixes(): void |
| 220 | 220 | { |
| 221 | 221 | $this->prefixes = []; |