In high-volume digital products such as games, social platforms or marketplaces, automated text moderation is a critical requirement for user experience and compliance. This article explores how the Aho–Corasick algorithm, implemented in Go, enables efficient and scalable detection of forbidden words, solving the performance limitations of traditional sequential-search approaches.

In digital environments where users constantly generate content names, comments, messages or form entries detecting forbidden words (NG words) is essential to ensure compliance, security and a consistent experience. From a product and platform perspective, this is not only a functional problem but an operational one: it must be solved in real time without degrading system performance. This post presents a solution based on the Aho–Corasick algorithm to optimize forbidden-word detection in a real system, showing how to move from a simple but costly implementation to an efficient, scalable architecture.
A basic prohibited-word detection implementation typically iterates the entire list of blocked terms and checks whether any appear in the input text.

Although easy to implement, the worst-case complexity of this approach is:
With high-traffic systems or long lists, this approach becomes infeasible from a performance standpoint.
From a product point of view the system must allow: Deny List: words that must be blocked if they appear in the text Allow List: explicit exceptions that override blocks Real-time evaluation without impacting latency
Deterministic behavior and ease of debugging from the admin panel

Aho–Corasick: efficient multi-pattern detection
The Aho–Corasick algorithm enables simultaneous search for multiple patterns in a text with search complexity O(n), independent of the number of forbidden words.
All deny and allow words are stored in a Trie structure, sharing common prefixes to reduce redundancy.
Each Trie node defines an alternative transition that lets the algorithm continue searching when there is no direct match, avoiding costly backtracking and preserving linear flow.
The text is scanned character by character. The Trie and failure transitions allow full matches to be detected without restarting the analysis.


This optimization is critical for products where moderation happens on every user interaction.
Using the Aho Corasick algorithm for forbidden-word detection transforms a seemingly simple problem into a robust, efficient, and scalable solution. By shifting complexity to initialization and keeping searches linear in time, it is possible to guarantee real-time moderation without compromising system performance or user experience.