3 ms·
You should be able to speed it up a bit by precomputing a prefix tree of all words. Then you can stop after three characters if there's no word in the tree that
by yorwba 6y ago
You should be able to speed it up a bit by precomputing a prefix tree of all words. Then you can stop after three characters if there's no word in the tree that begins with those three characters. That would also allow you to do away with the 5-character limit and recognize longer words like 中华人民共和国 without a performance penalty.
Humans do a bit more than simple prefix matching, though. E.g. most people won't read 有权利 as 有权/利 because that doesn't make much sense.