Count of sub-strings of length n possible from the given string - GeeksforGeeks
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
Given a string str and an integer N, the task is to find the number of possible sub-strings
of length N.
Examples :
Output : 9
Output : 3
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
Login Register
Below is the implementation of the above approach:
C++
// Driver code
int main()
{
string str = "geeksforgeeks";
int n = 5;
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
// Driver code
public static void main(String args[])
{
String str = "geeksforgeeks";
int n = 5;
System.out.print(countSubStr(str, n));
}
}
P ython3
length = len(string);
return (length - n + 1);
# Driver code
if __name__ == "__main__" :
string = "geeksforgeeks";
n = 5;
print(countSubStr(string, n));
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
class GFG
{
// Driver code
public static void Main()
{
string str = "geeksforgeeks";
int n = 5;
Console.WriteLine(countSubStr(str, n));
}
}
PHP
<?php
// PHP implementation of the approach
// 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
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
substrings of length 2K
11, Nov 20
03, Feb 22
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
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
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
News Languages
Top News Python
Technology Java
Work & Career CPP
Business Golang
Finance C#
Lifestyle SQL
Knowledge Kotlin
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