Verificare che un anagramma di una stringa sia contenuto in un’altra stringa in PHP

Verificare che un anagramma di una stringa sia contenuto in un’altra stringa in PHP

Mi è stato chiesto di creare una classe che abbia un metodo anagram che accetti due stringhe.
Il metodo deve controllare che la stringa $needle sia un qualsiasi anagramma dell'$haystack. Il metodo deve restituire true se si tratta di un anagramma, false viceversa

questa la definizione di partenza:

public function anagramm(string $needle, string $haystack): boolean {}

Le successive richieste erano:

  • Il codice sia sviluppato preferibilmente in PHP.
  • $needle sia una stringa di lunghezza massima di 1024 caratteri.
  • $haystack sia una stringa di lunghezza massima di 1024 caratteri.
  • Non ci siano funzioni native che effettuino l'anagramma di una stringa.
  • Il controllo sia case-insensitive.

### Esempio

Date due stringhe A = "abc" e B = "itookablackcab" lo script stamperà a video "vero", poichè anagrammando A si può trovare una occorrenza di "cab" nella stringa B

ECCO COME HO RISOLTO:

class Anagramma
{
private const MAX_NEEDLE = 1024;
private const MAX_HAYSTACK = 1024;

private $my_word_ori = null; // anagramma da cercare
private $my_word_el = null; // variabile da alterare
private $my_w_container = null; // dove cercare
private $msg = ''; // debug
private $anagrammFound = false; // esito finale

public function __construct() {
;
}

public function anagramm(string $needle, string $haystack): bool {
$this->my_word_ori = $this->my_word_el = $needle;
$this->my_w_container = $haystack;

// i parametri in in gresso sono corretti?
if (!$this->validateParameters()) {
return false;
}
$this->anagrammRecursive(0, strlen($this->my_word_ori));

return $this->anagrammFound;
}


/**
logic
*/
private function anagrammRecursive(int $lamp, int $len) : void {
if ($lamp == $len) {
// echo '*' . $this->my_word_el . "*" . PHP_EOL;
if (stripos($this->my_w_container, $this->my_word_el) !== false) {
$this->msg = 'CORRISPONDENZA TROVATA.' . "\n" . $this->my_word_el .
' (anagramma di ' . $this->my_word_ori . ') è contenuto in: ' . "\n" . $this->my_w_container . PHP_EOL;
$this->anagrammFound = true;
return;
}
}
else {
for ($j = $lamp; $j < $len; $j++) {
// echo 'lamp vale ' . $lamp . ' len vale ' . $len . PHP_EOL;
$temp = $this->my_word_el[$lamp];
$this->my_word_el[$lamp] = $this->my_word_el[$j];
$this->my_word_el[$j] = $temp;
$this->anagrammRecursive($lamp+1, $len);
// echo 'lamp2 vale ' . $lamp . ' len vale ' . $len . PHP_EOL;
$temp = $this->my_word_el[$lamp];
$this->my_word_el[$lamp] = $this->my_word_el[$j];
$this->my_word_el[$j] = $temp;
}
}
}

/**
verifico che i parametri in ingresso sia validi prima di procedere al confronto
- la loro lunghezza deve rientrare tra 1 e le costanti definite
- la parola da anagrammare non può essere più lunga della parola dove cercare
*/
private function validateParameters(): bool {
$l1 = strlen($this->my_word_el);
$l2 = strlen($this->my_w_container);

if ($l1 < 1 || $l1 > self::MAX_NEEDLE) {
$this->msg = 'Param 1 not in 1 and ' . self::MAX_NEEDLE . PHP_EOL;
return false;
}
if ($l2 < 1 || $l2 > self::MAX_HAYSTACK) {
$this->msg = 'Param 2 not in 1 and ' . self::MAX_HAYSTACK . PHP_EOL;
return false;
}
if ($l1 > $l2) {
$this->msg = 'Param 1 longer than param 2 ' . PHP_EOL;
return false;
}

return true;
}

public function debugMessage(): string {
if (empty($this->msg) && !$this->anagrammFound) {
$this->msg = 'CORRISPONDENZA NON TROVATA.' . "\n" . $this->my_word_ori . ' (anagrammato) non è contenuto in: ' .
"\n" . $this->my_w_container . PHP_EOL;
}
return $this->msg;
}

}
$cerca = '1234';
$cerca_in = 'sadff00dfdfdd3412f';
$obj_anagramma = new Anagramma();
$es = $obj_anagramma->anagramm($cerca, $cerca_in);
var_dump($es);
echo $obj_anagramma->debugMessage();
utilizzare strpos e substr in un template con smarty

come aumentare un contatore in pyhon in un template jinja

come usare un token a tempo per resettare la password in python

Begin, editor VI di linux. Che emozione

test audiometrico in python

validazione nell’utilizzo del modulo croniter di python