3v4l.org

run code in 300+ PHP versions simultaneously
<?php $combos = [ //Test case 1: normally written. ['FirstClass_1', 'FirstClass_2', 'FirstClass_3'], //Test case 2: swapped 'l' with 'C' inside 'FirstClass_1' to make it 'FirstlCass1'. ['FirstlCass_1', 'FirstClass_2', 'FirstClass_3'], //Test case 3: swapped positions. ['FirstClass_2', 'FirstClass_3', 'FirstlCass_1'] ]; class Node{ public $val; public $children; public $end_of_data; function __construct($val){ $this->val = $val; $this->children = []; $this->end_of_data = false; } } class DataManager{ private $root; private const INSERTED = 1; private const EXISTS = 2; function __construct(){ $this->root = new Node('~'); } public function putIfAbsent($data){ sort($data); // to make different combinations/anagrams of same values as one $node = $this->root; $status = self::EXISTS; foreach($data as $value){ if(!isset($node->children[$value])){ $node->children[$value] = new Node($value); $status = self::INSERTED; } $node = $node->children[$value]; } $node->end_of_data = true; return $status; } } $o = new DataManager(); foreach($combos as $combo){ var_dump($o->putIfAbsent($combo)); }
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 8.0.0 - 8.0.12, 8.1rc1 - rc3
int(1) int(1) int(2)

preferences:
35.45 ms | 460 KiB | 5 Q