A Trie maintains a set of strings in by maintaining a deterministic finite automaton that accepts and only accepts strings in . Specifically, let be a set of all prefixes of strings in , be a function in satisfying

Then it is easy to prove that is a deterministic finite automaton that accepts and only accepts strings in . This costs a space of .

Add

This is an algorithm that updates to in time and space.

Updating the values in that is changed by yields an algorithm that solves the problem in time and space.

int o = 1;
for (char c : s) {
	if (!next[o][c]) {
		next[o][c] = next.size();
		next.emplace_back();
		f.push_back(false);
	}
	o = next[o][c];
}
f[o] = true;

Find

This is an algorithm that checks if in time and space.

Running on yields an algorithm that solves the problem in time and space.

int o = 1;
for (char c : s) {
	o = next[o][c];
}
return f[o];