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.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
int(1) int(1) int(2)

preferences:
157.75 ms | 403 KiB | 175 Q