# Number of substrings of a string

Find total number of non-empty substrings of a string with N characters. Here we use the word proper because we do not consider string itself as part of output.

Input : str = “abc”
Output : 6
Proper substrings are “a”, “b”, “c”, “ab”, “bc”, “abc”

Input : str = “abcd”
Output : 10
Proper substrings are “a”, “b”, “c”, “d”, “ab”, “bc”, “cd”, “abc”, “bcd” and “abcd”

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Count of non-empty substrings is n*(n+1)/2

If we include empty string also as substring, the count becomes n*(n+1)/2 + 1

How does above formula work?

1. Number of substrings of length one is n (We can choose any of the n characters)
2. Number of substrings of length two is n-1 (We can choose any of the n-1 pairs formed by adjacent)
3. Number of substrings of length three is n-2
(We can choose any of the n-2 triplets formed by adjacent)
4. In general, mumber of substrings of length k is n-k+1 where 1 <= k <= n

Total number of substrings of all lengths from 1 to n =
n + (n-1) + (n-2) + (n-3) + … 2 + 1
= n * (n + 1)/2

## C++

 `// CPP program to count number of substrings ` `// of a string ` `#include ` `using` `namespace` `std; ` ` `  `int` `countNonEmptySubstr(string str) ` `{ ` `   ``int` `n = str.length(); ` `   ``return` `n*(n+1)/2; ` `} ` ` `  `// driver code ` `int` `main() ` `{ ` `    ``string s = ``"abcde"``; ` `    ``cout << countNonEmptySubstr(s); ` `    ``return` `0; ` `} `

## Java

 `// Java program to count number of substrings ` `// of a string ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` `     `  `    ``static` `int` `countNonEmptySubstr(String str) ` `    ``{ ` `        ``int` `n = str.length(); ` `        ``return` `n * (n + ``1``) / ``2``; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String s = ``"abcde"``; ` `        ``System.out.println( ` `                  ``countNonEmptySubstr(s)); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by Manish Shaw (manishshaw1) `

## Python3

 `# Python3 program to count number ` `# of substrings of a string ` ` `  `def` `countNonEmptySubstr(``str``): ` `    ``n ``=` `len``(``str``); ` `    ``return` `int``(n ``*` `(n ``+` `1``) ``/` `2``); ` ` `  `# driver code ` `s ``=` `"abcde"``; ` `print` `(countNonEmptySubstr(s)); ` ` `  `# This code is contributed by ` `# Manish Shaw (manishshaw1) `

## C#

 `// C# program to count number  ` `// of substrings of a string ` `using` `System; ` `class` `GFG { ` `     `  `    ``static` `int` `countNonEmptySubstr(``string` `str) ` `    ``{ ` `        ``int` `n = str.Length; ` `        ``return` `n * (n + 1) / 2; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``string` `s = ``"abcde"``; ` `        ``Console.Write(countNonEmptySubstr(s)); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by Manish Shaw (manishshaw1) `

## PHP

 ` `

Output:

```15
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : manishshaw1, vt_m

Article Tags :
Practice Tags :

8

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.