Anagrams
This commit is contained in:
parent
44a3ef971d
commit
53140ead8a
4 changed files with 341 additions and 159 deletions
27
src/Anagram.php
Normal file
27
src/Anagram.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
final class Anagram
|
||||
{
|
||||
private static function sortLettersInWord(string $word): string
|
||||
{
|
||||
return (new Collection(str_split($word)))
|
||||
->sort()
|
||||
->implode('');
|
||||
}
|
||||
|
||||
public static function forWord(string $word, array $candidates): Collection
|
||||
{
|
||||
$word = static::sortLettersInWord($word);
|
||||
|
||||
return (new Collection($candidates))
|
||||
->filter(fn (string $candidate): bool =>
|
||||
static::sortLettersInWord($candidate) == $word)
|
||||
->values();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue