Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Distinct-Substrings ) HackwithInfy Previous Questions | Distinct Substrings | THE INQUISITIVE





Distinct Substrings

LEVEL:Beginner

Description

Given a string, count all substrings of the given string.

Input Format

First line contains string S.

Output Format

Print the count of total substrings can form.


Example 1:

Input
abcd
Output
10
Example 2:

Input
aaa
Output
6
Example 3:

Input
abcde
Output
15

oops

Login to see Discussion




Approach

Step-1: Find the length of string
Step-2: Calculate total substrings value is length of string * (length of string +1)/2

Logic:
For every Stirng:
There will be n substring of length 1
there will be n-1 substring of length 2
...
there will be 1 substring of length n

Total substrings are 1+2+3+...+n = n(n+1)/2


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution