Method 1:Naive Approach
Approach:
The approach used in this code is to iterate over all possible starting positions of a substring of length N in the given string str, and count the number of valid substrings. We can check if a substring is valid by using the substr function to extract a substring of length N starting from the current position, and checking if its length is equal to N.
Implementation:
C++
#include <bits/stdc++.h> using namespace std; int count_substrings(string str, int n) { int count = 0; for ( int i = 0; i <= str.length() - n; i++) { string substring = str.substr(i, n); // check if the current substring has length n if (substring.length() == n) { count++; } } return count; } int main() { string str = "w3wiki" ; int n = 5; cout << count_substrings(str, n) << endl; return 0; } |
Java
public class CountSubstrings { public static int countSubstrings(String str, int n) { int count = 0 ; for ( int i = 0 ; i <= str.length() - n; i++) { String substring = str.substring(i, i + n); // check if the current substring has length n if (substring.length() == n) { count++; } } return count; } public static void main(String[] args) { String str = "w3wiki" ; int n = 5 ; System.out.println(countSubstrings(str, n)); } } |
Python3
def count_substrings(string, n): count = 0 for i in range ( len (string) - n + 1 ): substring = string[i:i + n] # check if the current substring has length n if len (substring) = = n: count + = 1 return count str = "w3wiki" n = 5 print (count_substrings( str , n)) |
C#
using System; class GFG { // Function to count the number of substrings of length n in a given string. static int CountSubstrings( string str, int n) { int count = 0; // Loop through the string starting from index 0 and going up to length - n. for ( int i = 0; i <= str.Length - n; i++) { // Get the substring of length n starting at index i. string substring = str.Substring(i, n); // Check if the current substring has length n. if (substring.Length == n) { count++; } } return count; } static void Main() { string str = "w3wiki" ; int n = 5; // Call the function to count substrings and print the result. Console.WriteLine(CountSubstrings(str, n)); } } |
Javascript
//Javascript Code function count_substrings(str, n) { let count = 0; for (let i = 0; i <= str.length - n; i++) { let substring = str.substr(i, n); // check if the current substring has length n if (substring.length === n) { count++; } } return count; } //Driver's Code let str = "w3wiki" ; let n = 5; console.log(count_substrings(str, n)); |
9
Time Complexity: O(M*N), where M is the length of the input string str, since we iterate over N possible starting positions and extract a substring of length N each time.
Auxiliary Space: O(1), no extra space is required.
Count of sub-strings of length n possible from the given string
Given a string str and an integer N, the task is to find the number of possible sub-strings of length N.
Examples:
Input: str = “w3wiki”, n = 5
Output: 9
All possible sub-strings of length 5 are “geeks”, “eeksf”, “eksfo”,
“ksfor”, “sforg”, “forge”, “orgee”, “rgeek” and “geeks”.
Input: str = “jgec”, N = 2
Output: 3