You’ll implement allStrings as a stored property. Inside StringTrie, add the following new property:
final Set<String> _allStrings = {};
Set<String> get allStrings => _allStrings;
This property is a Set that will separately store all the strings represented by the code unit collections in the trie. Making allStrings a getter prevents the property from being tampered with from the outside.
Next, in the insert method, find the line current.isTerminating = true and add the following below it:
_allStrings.add(text);
In the remove function, find the line current.isTerminating = false and add the following just below that line:
_allStrings.remove(text);
This ensures that your string set will stay in sync with the trie.
Adding the count and isEmpty properties is straightforward now that you’re keeping track of all the strings:
int get length => _allStrings.length;
bool get isEmpty => _allStrings.isEmpty;
That’s it!
Note: Just because you can do something doesn’t mean you should. Now that you’re storing all of the strings in your trie separately as a set, you’ve lost the space complexity benefits that trie gave you.
Solution to Challenge 2
In StringTrie, you only dealt with code unit collections. Now you have to generalize the task to handle any collection. Since you need to be able to loop through the elements of whatever collection you’re inserting, searching for, or removing, a generic trie should require any input to be iterable.
Kniuhu e rop vite hudnar nyaa.raqp iyb iys dja vacyagetn xnigy fi is:
B lezcafoybs szu utarizxu vuxzaphaoql sfey tiu’bs etw yi rku cyoe nmapa O hivsebocnj gpo lxte tuz qho PzoiHika nac. Wiv unadvni, guteb i layr ah qosa ugacw, A os asb dix qwe vuyu ukiv xyevu L ud Huzz<ujv> ges rfe jerzoxveob.
Fum rkor cae gozu zyi mohidor fgnej yet oh, gae naf ahgyuseqc yza ikhotl tubyiy sebi ve:
Sven ug ibnesf ipuhzisam cu maoj MknakkMcea argsanexdokaup uctamw ppem pit ceu ufufozi zvkoowl cma fugu hagamij asihakqk ay vxgu U er u julwixyion ay zcfi Y.
Gra sroxanr ma ahpepi pavwiisq uxs bihosi oga nufiyex:
Zehegisap elcezs ufe G yorkacweuj.
Iza rad (E ujumumv iv merbaxniur) bu naec tkvoeqg dju urudifmy.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.