Number of substrings of a string

31 May, 2022
Find total number of non-empty substrings of a string with N characters. 
Input : str = “abc” Output : 6 Every substring of the given string : “a”, “b”, “c”, “ab”, “bc”, “abc”
Input : str = “abcd” Output : 10 Every substring of the given string : “a”, “b”, “c”, “d”, “ab”, “bc”, “cd”, “abc”, “bcd” and “abcd”
Count of non-empty substrings is n*(n+1)/2If we include empty string also as substring, the count becomes n*(n+1)/2 + 1
How does above formula work?  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Number of substrings of length one is n (We can choose any of the n characters)Number of substrings of length two is n-1 (We can choose any of the n-1 pairs formed by adjacent)Number of substrings of length three is n-2 (We can choose any of the n-2 triplets formed by adjacent)In general, number of substrings of length k is n-k+1 where 1 <= k <= n
Number of substrings of length one is n (We can choose any of the n characters)
Number of substrings of length two is n-1 (We can choose any of the n-1 pairs formed by adjacent)
Number of substrings of length three is n-2 (We can choose any of the n-2 triplets formed by adjacent)
In general, number 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++
Java
Python3
C#
PHP
Javascript
// CPP program to count number of substrings// of a string#include <bits/stdc++.h>using namespace std; int countNonEmptySubstr(string str){   int n = str.length();   return n*(n+1)/2;} // driver codeint main(){    string s = "abcde";    cout << countNonEmptySubstr(s);    return 0;}
// Java program to count number of substrings// of a stringimport 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 program to count number# of substrings of a string def countNonEmptySubstr(str):    n = len(str);    return int(n * (n + 1) / 2); # driver codes = "abcde";print (countNonEmptySubstr(s)); # This code is contributed by# Manish Shaw (manishshaw1)
// C# program to count number// of substrings of a stringusing 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// PHP program to count number// of substrings of a string function countNonEmptySubstr($str){    $n = strlen($str);    return $n * ($n + 1) / 2;} // Driver Code$s = "abcde";echo countNonEmptySubstr($s);     // This code is contributed by Anuj_67?>
<script> // JavaScript program to count number of substrings// of a stringfunction countNonEmptySubstr(str)    {        let n = str.length;        return n * (n + 1) / 2;    }         // Driver code        let s = "abcde";        document.write(countNonEmptySubstr(s));  // This code is contributed shivanisinghss2110 </script>
15
 
Time Complexity: O(1).
Auxiliary Space: O(1).
https://youtu.be/9QxJo
-g0cMA 
manishshaw1
vt_m
praveenkumar14
surinderdawra388
shivanisinghss2110
anandkumarshivam2266
Mathematical
Strings
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.