|
| 1 | +/* |
| 2 | +Count Distinct Substrings |
| 3 | +Send Feedback |
| 4 | +Given a string 'S', you are supposed to return the number of distinct substrings(including empty substring) of the given string. You should implement the program using a trie. |
| 5 | +Note : |
| 6 | +A string ‘B’ is a substring of a string ‘A’ if ‘B’ that can be obtained by deletion of, several characters(possibly none) from the start of ‘A’ and several characters(possibly none) from the end of ‘A’. |
| 7 | +
|
| 8 | +Two strings ‘X’ and ‘Y’ are considered different if there is at least one index ‘i’ such that the character of ‘X’ at index ‘i’ is different from the character of ‘Y’ at index ‘i’(X[i]!=Y[i]). |
| 9 | +Input Format : |
| 10 | +The first line contains a single integer ‘T’ denoting the number of test cases. |
| 11 | +
|
| 12 | +Then, the ‘T’ test cases follow. |
| 13 | +
|
| 14 | +The first line of each test case contains a string. |
| 15 | +Output Format : |
| 16 | +For each test case, print an integer denoting the number of distinct substrings in the given string. |
| 17 | +
|
| 18 | +The output for each test case will be printed in a separate line. |
| 19 | +Note : |
| 20 | +You don’t need to print anything, It has already been taken care of. Just implement the given function. |
| 21 | +Constraints : |
| 22 | +1 <= T <= 5 |
| 23 | +1 <= |S| <= 10^3 |
| 24 | +
|
| 25 | +‘S’ contains only lowercase English letters. |
| 26 | +
|
| 27 | +Time Limit: 1 sec |
| 28 | +Sample Input 1 : |
| 29 | +2 |
| 30 | +sds |
| 31 | +abc |
| 32 | +Sample Output 1 : |
| 33 | +6 |
| 34 | +7 |
| 35 | +Explanation of Sample Input 1 : |
| 36 | +In the first test case, the 6 distinct substrings are { ‘s’,’ d’, ”sd”, ”ds”, ”sds”, “” } |
| 37 | +
|
| 38 | +In the second test case, the 7 distinct substrings are {‘a’, ‘b’, ‘c’, “ab”, “bc”, “abc”, “” }. |
| 39 | +Sample Input 2 : |
| 40 | +2 |
| 41 | +aa |
| 42 | +abab |
| 43 | +Sample Output 2 : |
| 44 | +3 |
| 45 | +8 |
| 46 | +Explanation of Sample Input 2 : |
| 47 | +In the first test case, the two distinct substrings are {‘a’, “aa”, “” }. |
| 48 | +
|
| 49 | +In the second test case, the seven distinct substrings are {‘a’, ‘b’, “ab”, “ba”, “aba”, “bab”, “abab”, “” } |
| 50 | +*/ |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +/* |
| 55 | + Time Complexity: O(N^2) |
| 56 | + Space Complexity: O(N^2) |
| 57 | +
|
| 58 | + Where 'N' is the length of the given string. |
| 59 | +*/ |
| 60 | + |
| 61 | +// Class for making trie |
| 62 | +class TrieNode |
| 63 | +{ |
| 64 | +public: |
| 65 | + TrieNode *children[26]; |
| 66 | + |
| 67 | + TrieNode() |
| 68 | + { |
| 69 | + |
| 70 | + for (int i = 0; i < 26; i++) |
| 71 | + { |
| 72 | + children[i] = NULL; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + ~TrieNode() |
| 77 | + { |
| 78 | + for (int i = 0; i < 26; i++) |
| 79 | + { |
| 80 | + if (children[i] != NULL) |
| 81 | + { |
| 82 | + delete children[i]; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +// Function to insert a string into the trie. |
| 89 | +void insert(string &s, int i, TrieNode *head) |
| 90 | +{ |
| 91 | + TrieNode *temp = head; |
| 92 | + |
| 93 | + int n = s.length(); |
| 94 | + |
| 95 | + // Iterate through the given string |
| 96 | + while (i < n) |
| 97 | + { |
| 98 | + char ch = s[i]; |
| 99 | + |
| 100 | + // If child node is not present. |
| 101 | + if (temp->children[ch - 'a'] == NULL) |
| 102 | + { |
| 103 | + // Create new child node. |
| 104 | + temp->children[ch - 'a'] = new TrieNode(); |
| 105 | + } |
| 106 | + |
| 107 | + // Move to the child node. |
| 108 | + temp = temp->children[ch - 'a']; |
| 109 | + i++; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Function to count the number of nodes in the trie. |
| 114 | +int countNodes(TrieNode *root) |
| 115 | +{ |
| 116 | + if (root == NULL) |
| 117 | + { |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + int subTreeNodes = 1; |
| 122 | + |
| 123 | + // Iterate through the children of the current node. |
| 124 | + for (int i = 0; i < 26; i++) |
| 125 | + { |
| 126 | + subTreeNodes += countNodes(root->children[i]); |
| 127 | + } |
| 128 | + |
| 129 | + return subTreeNodes; |
| 130 | +} |
| 131 | + |
| 132 | +int countDistinctSubstrings(string &s) |
| 133 | +{ |
| 134 | + int n = s.length(); |
| 135 | + |
| 136 | + // Intialize the root of the trie. |
| 137 | + TrieNode *head = new TrieNode(); |
| 138 | + |
| 139 | + // Insert all suffixes into the trie. |
| 140 | + for (int i = 0; i < n; i++) |
| 141 | + { |
| 142 | + |
| 143 | + insert(s, i, head); |
| 144 | + } |
| 145 | + |
| 146 | + // Count the total number of nodes in the trie. |
| 147 | + int totalNodes = countNodes(head); |
| 148 | + |
| 149 | + // Free the memory. |
| 150 | + delete head; |
| 151 | + |
| 152 | + // Return the number of distinct substrings. |
| 153 | + return totalNodes; |
| 154 | +} |
0 commit comments