1) By General Method
In this method, we first calculate all the possible substring by using nested for loops. After that count all the substring that we calculated. But this process is time-consuming and takes a lot of time if the length of the string exceeds too much.
Time Complexity : O(n*n) , n is the length of the string
2) By Formula
In this method, we can simply calculate the total number of possible substrings by a formula.
Total number of substrings:
n + (n-1) + (n-2) + ... + 3 + 2 + 1
= n * (n + 1) / 2
Python code to count number of substrings of a string
Output
need an explanation for this answer? contact us directly to get an explanation for this answer