> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/overview/2.-data-structures/trie/java.md).

# Java

[LeetCode - The World's Leading Online Programming Learning Platform](https://leetcode.com/problems/word-search-ii/solutions/3835399/java-99-faster-dfs-with-trie/)

```jsx

    private static final class Trie {
        private static final int R = 26;

        private final Trie parent;

        private String word;
        private int counter;
        private char val;

        private final Trie[] nexts = new Trie[R];

        public Trie(Trie parent) {
            this.parent = parent;
        }

        public void add(String s) {
            Trie curr = this;
            for (int i=0; i<s.length(); i++) {
                curr.add(s.charAt(i));
                curr = curr.get(s.charAt(i));
            }
            curr.word = s;
        }

        public void remove(String s) {
            word = null;

            var curr = this;
            for (int i=s.length() - 1; i>=0; i--) {
                curr.counter--;
                var parent = curr.parent;
                if (curr.counter == 0) {
                    parent.nexts[curr.val - 'a'] = null;
                }

                curr = parent;
            }
        }

        public boolean isWord() {
            return word != null;
        }

        public String getWord() {
            return word;
        }

        public void addAll(String[] ss) {
            for (String s : ss) {
                add(s);
            }
        }

        private void add(char c) {
            if (!contains(c)) {
                nexts[c - 'a'] = new Trie(this);
                nexts[c - 'a'].val = c;
            }
            nexts[c - 'a'].counter++;
        }

        private boolean contains(char c) {
            return nexts[c - 'a'] != null
                && nexts[c - 'a'].counter != 0;
        }

        private Trie get(char c) {
            return nexts[c - 'a'];
        }
    }
}
```

```java
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://algorithm.prettylog.com/overview/2.-data-structures/trie/java.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
