> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/trees/shortest-unique-prefix.md).

# Shortest Unique Prefix

Find shortest unique prefix to represent each word in the list.

**Example:**

```
Input: [zebra, dog, duck, dove]
Output: {z, dog, du, dov}
where we can see that
zebra = z
dog = dog
duck = du
dove = dov
```

```java
public class Solution {
    static class TrieNode {
        char data;
        boolean isTerminating;
        TrieNode children[];
        int wordsPassingthrough;

        public TrieNode(char data) {
            this.data = data;
            isTerminating = false;
            children = new TrieNode[26];
            wordsPassingthrough = 0;
        }
    }

    static public class Trie {
        TrieNode root;
        public int count;

        public Trie() {
            root = new TrieNode('\0');
        }

        public int search(String word) {
            return search(root, word, -1);
        }

        int search(TrieNode node, String word, int level) {
            if (word.length() == 0)
                return level;
            if (node.wordsPassingthrough == 1 && node != this.root)
                return level;
            int childIndex = word.charAt(0) - 'a';
            return search(node.children[childIndex], word.substring(1), level + 1);
        }

        public void add(String word) {
            add(root, word);
        }

        void add(TrieNode root, String word) {
            if (word.length() == 0) {
                root.isTerminating = true;
                return;
            }
            int childIndex = word.charAt(0) - 'a';
            if (root.children[childIndex] == null)
                root.children[childIndex] = new TrieNode(word.charAt(0));
            root.wordsPassingthrough++;
            add(root.children[childIndex], word.substring(1));
        }
    }

    public String[] prefix(String[] A) {
        Trie trie = new Trie();
        for (String x : A)
            trie.add(x);
        String[] ans = new String[A.length];
        for (int i = 0; i < A.length; i++) {
            int index = trie.search(A[i]);
            ans[i] = A[i].substring(0, index + 1);
        }
        return ans;
    }
}
```


---

# 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:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/trees/shortest-unique-prefix.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
