Number of substrings of a string Last Updated : 09 May, 2023 Comments Improve Suggest changes 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). Comment More infoAdvertise with us Next Article Number of substrings of a string 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 Substring meaning in DSA A substring is defined as a contiguous part of a string, i.e., a string inside another string. Substrings of String "geeks"Characteristics of Substring: Starting position of a substring is greater than or equal to the starting index of the string and the ending position is less than or equal to the 2 min read Count number of substrings of a string consisting of same characters Given a string. The task is to find out the number of substrings consisting of the same characters. Examples: Input: abba Output: 5 The desired substrings are {a}, {b}, {b}, {a}, {bb} Input: bbbcbb Output: 10 Approach: It is known for a string of length n, there are a total of n*(n+1)/2 number of su 6 min read Count of substrings of a binary string containing K ones Given a binary string of length N and an integer K, we need to find out how many substrings of this string are exist which contains exactly K ones. Examples: Input : s = â10010â K = 1 Output : 9 The 9 substrings containing one 1 are, â1â, â10â, â100â, â001â, â01â, â1â, â10â, â0010â and â010âRecommen 7 min read Count of Reverse Bitonic Substrings in a given String Given a string S, the task is to count the number of Reverse Bitonic Substrings in the given string. Reverse bitonic substring: A string in which the ASCII values of the characters of the string follow any of the following patterns: Strictly IncreasingStrictly decreasingDecreasing and then increasin 8 min read Like