LeetCode 1698: Number of Distinct Substrings in a String Solution

Master LeetCode problem 1698 (Number of Distinct Substrings in a String), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1698. Number of Distinct Substrings in a String

Problem Explanation

Explanation

To find the number of distinct substrings in a given string, we can use a trie data structure. A trie is a tree-like data structure that is used to store a dynamic set where the keys are usually strings. Each node in the trie represents a common prefix of its child nodes.

The idea is to iterate through all substrings of the given string and insert each substring into the trie. The number of distinct substrings will be equal to the number of nodes in the trie. We can maintain a count of the total number of nodes created during the insertion process.

Here's how we can solve this problem step by step:

  1. Initialize a trie data structure.
  2. Iterate through all substrings of the input string.
  3. For each substring, insert it into the trie.
  4. Count the total number of nodes in the trie, which represents the number of distinct substrings.

Time complexity: O(n^2) where n is the length of the input string. This is because there are O(n^2) substrings possible in a string of length n. Space complexity: O(n^2) for storing all substrings in the trie.

Solution Code

class TrieNode {
    TrieNode[] children;
    boolean isEnd;

    TrieNode() {
        children = new TrieNode[26];
        isEnd = false;
    }
}

class Solution {
    public int countDistinct(String s) {
        TrieNode root = new TrieNode();
        int count = 0;

        for (int i = 0; i < s.length(); i++) {
            TrieNode curr = root;
            for (int j = i; j < s.length(); j++) {
                if (curr.children[s.charAt(j) - 'a'] == null) {
                    curr.children[s.charAt(j) - 'a'] = new TrieNode();
                    count++;
                }
                curr = curr.children[s.charAt(j) - 'a'];
            }
        }

        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1698 (Number of Distinct Substrings in a String)?

This page provides optimized solutions for LeetCode problem 1698 (Number of Distinct Substrings in a String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1698 (Number of Distinct Substrings in a String)?

The time complexity for LeetCode 1698 (Number of Distinct Substrings in a String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1698 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1698 in Java, C++, or Python.

Back to LeetCode Solutions