0% found this document useful (0 votes)
4 views

Count of sub-strings of length n possible from the given string - GeeksforGeeks

The document explains how to count the number of possible substrings of a given length N from a string. The formula used is len - n + 1, where len is the length of the string. It provides examples and implementations in multiple programming languages including C++, Java, Python, C#, PHP, and JavaScript.

Uploaded by

23b01a1253
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Count of sub-strings of length n possible from the given string - GeeksforGeeks

The document explains how to count the number of possible substrings of a given length N from a string. The formula used is len - n + 1, where len is the length of the string. It provides examples and implementations in multiple programming languages including C++, Java, Python, C#, PHP, and JavaScript.

Uploaded by

23b01a1253
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

9/21/22, 10:48 PM Count of sub-strings of length n possible from the given string - GeeksforGeeks

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python C

Count of sub-strings of length n possible from


the given string
Last Updated : 07 May, 2021

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 = “geeksforgeeks”, 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

Recommended: Please tr y your approach on {IDE} first, before moving on to the

solution.

Approach: The count of sub-strings of length n will always be len – n + 1 where len is the

length of the given string. For example, if str = “geeksforgeeks” and n = 5 then the count
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site,sub-strings
of you acknowledgehaving
that you have read 5
length and understood
will our Cookie
be “geeks”, Policy &”,
“eeksf Privacy Policy “ksfor ”,
“eksfo”, “sforg”,

https://www.geeksforgeeks.org/count-of-sub-strings-of-length-n-possible-from-the-given-string/ 1/8
9/21/22, 10:48 PM Count of sub-strings of length n possible from the given string - GeeksforGeeks

Start Your Coding Journey Now!


“forge”, “orgee”, “rgeek ” and “geeks” which is len – n + 1 = 13 – 5 + 1 = 9.

Login Register
Below is the implementation of the above approach:

Data Analyst online


course

C++

// C++ implementation of the approach


#include <bits/stdc++.h>
using namespace std;

// Function to return the count of


// possible sub-strings of length n
int countSubStr(string str, int n)
{
int len = str.length();
return (len - n + 1);
}

// Driver code
int main()
{
string str = "geeksforgeeks";
int n = 5;

cout << countSubStr(str, n);

return 0;
}

Java
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/count-of-sub-strings-of-length-n-possible-from-the-given-string/ 2/8
9/21/22, 10:48 PM Count of sub-strings of length n possible from the given string - GeeksforGeeks

// Java implementation of the approach


Start Your Coding Journey Now!
import java.util.*; Login Register
class GFG
{

// Function to return the count of


// possible sub-strings of length n
static int countSubStr(String str, int n)
{
int len = str.length();
return (len - n + 1);
}

// Driver code
public static void main(String args[])
{
String str = "geeksforgeeks";
int n = 5;

System.out.print(countSubStr(str, n));
}
}

// This code is contributed by mohit kumar 29

P ython3

# Python3 implementation of the approach

# Function to return the count of


# possible sub-strings of length n
def countSubStr(string, n) :

length = len(string);
return (length - n + 1);

# Driver code
if __name__ == "__main__" :

string = "geeksforgeeks";
n = 5;

print(countSubStr(string, n));

# This code is contributed by Ryuga


We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/count-of-sub-strings-of-length-n-possible-from-the-given-string/ 3/8
9/21/22, 10:48 PM Count of sub-strings of length n possible from the given string - GeeksforGeeks

C#Start Your Coding Journey Now! Login Register

// C# implementation of the approach


using System;

class GFG
{

// Function to return the count of


// possible sub-strings of length n
static int countSubStr(string str, int n)
{
int len = str.Length;
return (len - n + 1);
}

// Driver code
public static void Main()
{
string str = "geeksforgeeks";
int n = 5;

Console.WriteLine(countSubStr(str, n));
}
}

// This code is contributed by Code_Mech.

PHP

<?php
// PHP implementation of the approach

// Function to return the count of


// possible sub-strings of length n
function countSubStr($str, $n)
{
$len = strlen($str);
return ($len - $n + 1);
}

// Driver code
$str = "geeksforgeeks";
$n = 5;
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
echo(countSubStr($str,
site, $n));
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/count-of-sub-strings-of-length-n-possible-from-the-given-string/ 4/8
9/21/22, 10:48 PM Count of sub-strings of length n possible from the given string - GeeksforGeeks

Start Your Coding Journey Now!


// This code is contributed by Code_Mech.
?>
Login Register

Javascript

<script>
// JavaScript implementation of the approach
// Function to return the count of
// possible sub-strings of length n
function countSubStr(str, n) {
var len = str.length;
return len - n + 1;
}

// Driver code
var str = "geeksforgeeks";
var n = 5;

document.write(countSubStr(str, n));
</script>

Output :

Like 1

We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Previous Next

https://www.geeksforgeeks.org/count-of-sub-strings-of-length-n-possible-from-the-given-string/ 5/8
9/21/22, 10:48 PM Count of sub-strings of length n possible from the given string - GeeksforGeeks

Start Your Coding Journey Now! Login Register

RECOMMENDED ARTICLES Page : 1 2 3

Lexicographically smallest Check if all substrings of length K of


01 05
permutation of a string that can be a Binary String has equal count of
reduced to length K by removing K- 0s and 1s
length prefixes from palindromic 08, Sep 20

substrings of length 2K
11, Nov 20

Count binary strings of length same 06 Count M-length substrings


02
as given string after removal of occurring exactly K times in a string
substrings "01" and "00" that 19, Feb 21

consists of at least one '1'


18, Nov 20

Count ways to split string into K


Count of setbits in bitwise OR of all 07
Substrings starting with even digit
03
K length substrings of given Binary and min length M
String 19, Sep 22

03, Feb 22

Minimum length of Run Length


04
Encoding possible by removing at Count of substrings of a string
08
most K characters from a given containing another given string as a
string substring
25, Oct 20 03, Dec 20

Ar ticle Contributed By :

bitan2017
We use cookies to ensure you have the best browsing experience on our website. By using our
@bitan2017 Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/count-of-sub-strings-of-length-n-possible-from-the-given-string/ 6/8
9/21/22, 10:48 PM Count of sub-strings of length n possible from the given string - GeeksforGeeks

Start Your Coding Journey Now! Login Register


Vote for difficulty

Easy Normal Medium Hard Expert

Improved By : ankthon, mohit kumar 29, Code_Mech, rdtank

Article Tags : substring, C++ Programs, Strings

Practice Tags : Strings

Improve Article Report Issue

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

A-143, 9th Floor, Sovereign Corporate Tower,


Sector-136, Noida, Uttar Pradesh - 201305
[email protected]

Company Learn
About Us Algorithms
Careers Data Structures
We use cookies to ensure In
youMedia SDEourCheat
have the best browsing experience on our website. By using SheetGot It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/count-of-sub-strings-of-length-n-possible-from-the-given-string/ 7/8
9/21/22, 10:48 PM Count of sub-strings of length n possible from the given string - GeeksforGeeks

Contact Us Machine learning


Start YourPrivacy
Coding PolicyJourney Now!
Login
CS Subjects
Register

Copyright Policy Video Tutorials


Courses

News Languages
Top News Python
Technology Java
Work & Career CPP
Business Golang
Finance C#
Lifestyle SQL
Knowledge Kotlin

Web Development Contribute


Web Tutorials Write an Article
Django Tutorial Improve an Article
HTML Pick Topics to Write
JavaScript Write Interview Experience
Bootstrap Internships
ReactJS Video Internship
NodeJS

@geeksforgeeks , Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/count-of-sub-strings-of-length-n-possible-from-the-given-string/ 8/8

You might also like