Number of substrings of a string Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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? 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 Implementation: C++ // 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 code int main() { string s = "abcde"; cout << countNonEmptySubstr(s); return 0; } C #include <stdio.h> #include <string.h> int countNonEmptySubstr(const char* str) { int n = strlen(str); return n * (n + 1) / 2; } // driver code int main() { const char* s = "abcde"; printf("%d\n", 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 <?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 ?> JavaScript <script> // JavaScript program to count number of substrings // of a string function 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> Output: 15 Complexity Analysis: Time Complexity: O(1).Auxiliary Space: O(1). Number of substrings of a string Comment More infoAdvertise with us Next Article Sum of all substrings of a string representing a number | Set 1 S Shashank_Pathak Follow Improve Article Tags : Strings Mathematical DSA Practice Tags : MathematicalStrings Similar Reads Number of even substrings in a string of digits Given a string of digits 0 - 9. The task is to count a number of substrings which when converting into integer form an even number. Examples : Input : str = "1234".Output : 6"2", "4", "12", "34", "234", "1234" are 6 substring which are even.Input : str = "154".Output : 3Input : str = "15".Output : 0 9 min read Number of substrings divisible by 4 in a string of integers Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when converted into integer are divisible by 4. Substring may contain leading zeroes. Examples: Input : "124" Output : 4 Substrings divisible by 4 are "12", "4", "24", "124" . Input : "04" Output : 3 Su 10 min read Sum of all substrings of a string representing a number | Set 1 Given an integer represented as a string, we need to get the sum of all possible substrings of this string.Note: It is guaranteed that sum of all substring will fit within a 32-bit integer.Examples: Input: s = "6759"Output: 8421Explanation: sum = 6 + 7 + 5 + 9 + 67 + 75 + 59 + 675 + 759 + 6759 = 842 14 min read Find the longest Substring of a given String S Given a string S of length, N. Find the maximum length of any substring of S such that, the bitwise OR of all the characters of the substring is equal to the bitwise OR of the remaining characters of the string. If no such substring exists, print -1. Examples: Input: S = "2347"Output: 3?Explanation: 10 min read Number of substrings with odd decimal value in a binary string Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Subst 6 min read Min Length String with All Substrings of Size N Given two integers n and k, your task is to find a string of minimum length to contain all possible strings of size n as a substring. The characters of the string should be integers ranging from 0 to k - 1. Examples:Input: n = 2, k = 2Output: 00110Explanation: Allowed characters are from 0 to k-1 (i 7 min read Like