Count Number of Distinct Substrings in a String in JavaA string inStr is given to us. The task is to compute the total number of unique substrings of the string inStr. All the characters of the input string are lowercase alphabets. Example 1: Input String inStr = "abcde" Output: There are 16 unique substrings. Explanation: The distinct substrings are: "", "a", "b", "c", "d", "e", "ab", "bc", "cd", "de", "abc", "bcd", "cde", "abcd", "bcde", "abcde" and their count is 16. Hence, the output is 16. Example 2: Input String inStr = "abbcccdd" Output: There are 32 unique substrings. Explanation: The distinct substrings are: "", "a", "b", "c", "d", "ab", "bb", "bc", "cc", "cd", "dd", "abb", "bbc", "bcc", "ccc", "ccd", "cdd", "abbc", "bbcc", "bccc", "cccd", "ccdd", "abbcc", "bbccc" "bcccd", "cccdd", "abbccc", "bbcccd", "bcccdd", "abbcccd", "bbcccdd", "abbcccdd"and their count is 32. Hence, the output is 32. Example 3: Input String inStr = "aaaaaaa" Output: There are 8 unique substrings. Explanation: The distinct substrings are: "", "a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa" and their count is 8. Hence, the output is 8. Approach: Using Substring() MethodThe approach is simple. All we need to do is to generate all of the substrings of the given string using nested for-loops and the substring() method. The generated substrings can be put in a hash set. Thus, all duplicate substrings will be discarded as a hash set always containing the unique elements. The size of the hash set is our answer. FileName: DistinctSubArrCount.java Output: For the string: "abcde", the total substring count is: 16 For the string: "abbcccdd", the total substring count is: 32 For the string: "aaaaaaa", the total substring count is: 8 Complexity Analysis: The nested for-loops take the O(n^{2}) time to do their work. Also, we are invoking the method substring() in the inner for-loop, and the substring() method takes O(n) time. Therefore, the total time complexity of the program is O(n^{3}). The program is also using a hash set for storing the substrings. The number of strings can go up to (n x (n + 1)) / 2, and the size of a substring can go up to n. Therefore, the space complexity of the program is O(n^{3}), where n is the total number of characters present in the input string. Optimization: We can exclude the usage of the method substring() to reduce the time complexity of the program. We can append the current character to the previous substring to generate the current substring. FileName: DistinctSubArrCount1.java Output: For the string: "abcde", the total substring count is: 16 For the string: "abbcccdd", the total substring count is: 32 For the string: "aaaaaaa", the total substring count is: 8 Complexity Analysis: The program uses only nested for-loops. Thus, making the time complexity of the program O(n^{2}). The space complexity remains the same as the previous program. The above programs are not good for larger strings because of the higher space complexity. For large strings, the above programs give MLE (Memory Limit Exceeded). Therefore, it is required to do some optimization to reduce the memory consumed by the program. Approach: Using TRIEThe improvement in space complexity can be made using TRIE. The approach is to only insert those characters in the TRIE that have not been inserted before. When such a character occurs then it means we are going to get the unique substring and we increase the count of the unique substrings by 1. Observe the following implementation. FileName: DistinctSubArrCount2.java Output: For the string: "abcde", the total substring count is: 16 For the string: "abbcccdd", the total substring count is: 32 For the string: "aaaaaaa", the total substring count is: 8 Complexity Analysis: The time complexity of the program remains the same as the previous one. For every character that has not been stored in the data structure TRIE, we need to allocate the memory of 26 characters, which makes the space complexity of the program O (26 x n), which we can also write asymptotically as O(n). |