# Different substrings in a string that start and end with given strings

Given a string s and two other strings begin and end, find the number of different substrings in the string which begin and end with the given begin and end strings.

**Examples:**

Input : s = "geeksforgeeks" begin = "geeks" end = "for" Output : 1 Input : s = "vishakha" begin = "h" end = "a" Output : 2 Two different sub-strings are "ha" and "hakha".

**Approach : ** Find all occurrences of string begin and string end. Store the index of each string in two different arrays. After that traverse through whole string and add one symbol per iteration to already seen sub-strings and map new strings to some non-negative integers. As the ends and beginnings of strings and different string of equal length are mapped to different numbers (and equal strings are mapped equally), simply count the number of necessary sub-strings of certain length.

## C++

`// Cpp program to find number of ` `// different sub stings ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to return number of different ` `// sub-strings ` `int` `numberOfDifferentSubstrings(string s, string a, ` ` ` `string b) ` `{ ` ` ` `// initially our answer is zero. ` ` ` `int` `ans = 0; ` ` ` ` ` `// find the length of given strings ` ` ` `int` `ls = s.size(), la = a.size(), lb = b.size(); ` ` ` ` ` `// currently make array and initially put zero. ` ` ` `int` `x[ls] = { 0 }, y[ls] = { 0 }; ` ` ` ` ` `// find occurence of "a" and "b" in string "s" ` ` ` `for` `(` `int` `i = 0; i < ls; i++) { ` ` ` `if` `(s.substr(i, la) == a) ` ` ` `x[i] = 1; ` ` ` `if` `(s.substr(i, lb) == b) ` ` ` `y[i] = 1; ` ` ` `} ` ` ` ` ` `// We use a hash to make sure that same ` ` ` `// substring is not counted twice. ` ` ` `unordered_set<string> hash; ` ` ` ` ` `// go through all the positions to find ` ` ` `// occurrence of "a" first. ` ` ` `string curr_substr = ` `""` `; ` ` ` `for` `(` `int` `i = 0; i < ls; i++) { ` ` ` ` ` `// if we found occurrence of "a". ` ` ` `if` `(x[i]) { ` ` ` ` ` `// then go through all the positions ` ` ` `// to find occurrence of "b". ` ` ` `for` `(` `int` `j = i; j < ls; j++) { ` ` ` ` ` `// if we do found "b" at index ` ` ` `// j then add it to already ` ` ` `// existed substring. ` ` ` `if` `(!y[j]) ` ` ` `curr_substr += s[j]; ` ` ` ` ` `// if we found occurrence of "b". ` ` ` `if` `(y[j]) { ` ` ` ` ` `// now add string "b" to ` ` ` `// already existed substing. ` ` ` `curr_substr += s.substr(j, lb); ` ` ` ` ` `// If current substring is not ` ` ` `// included already. ` ` ` `if` `(hash.find(curr_substr) == hash.end()) ` ` ` `ans++; ` ` ` ` ` `// put any non negative ` ` ` `// integer to make this ` ` ` `// string as already ` ` ` `// existed. ` ` ` `hash.insert(curr_substr); ` ` ` `} ` ` ` `} ` ` ` ` ` `// make substring null. ` ` ` `curr_substr = ` `""` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// return answer. ` ` ` `return` `ans; ` `} ` ` ` `// Driver program for above function. ` `int` `main() ` `{ ` ` ` `string s = ` `"codecppforfood"` `; ` ` ` `string begin = ` `"c"` `; ` ` ` `string end = ` `"d"` `; ` ` ` `cout << numberOfDifferentSubstrings(s, begin, end) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python 3

# Python 3 program to find number of

# different sub stings

# function to return number of different

# sub-strings

def numberOfDifferentSubstrings(s, a, b):

# initially our answer is zero.

ans = 0

# find the length of given strings

ls = len(s)

la = len(a)

lb = len(b)

# currently make array and initially

# put zero.

x = [0] * ls

y = [0] * ls

# find occurence of “a” and “b” in string “s”

for i in range(ls):

if (s[i: la + i] == a):

x[i] = 1

if (s[i: lb + i] == b):

y[i] = 1

# We use a hash to make sure that same

# substring is not counted twice.

hash = []

# go through all the positions to find

# occurrence of “a” first.

curr_substr = “”

for i in range(ls):

# if we found occurrence of “a”.

if (x[i]):

# then go through all the positions

# to find occurrence of “b”.

for j in range( i, ls):

# if we do found “b” at index

# j then add it to already

# existed substring.

if (not y[j]):

curr_substr += s[j]

# if we found occurrence of “b”.

if (y[j]):

# now add string “b” to

# already existed substing.

curr_substr += s[j: lb + j]

# If current substring is not

# included already.

if curr_substr not in hash:

ans += 1

# put any non negative integer

# to make this string as already

# existed.

hash.append(curr_substr)

# make substring null.

curr_substr = “”

# return answer.

return ans

# Driver Code

if __name__ == “__main__”:

s = “codecppforfood”

begin = “c”

end = “d”

print(numberOfDifferentSubstrings(s, begin, end))

# This code is contributed by ita_c

**Output:**

3

## Recommended Posts:

- Given a binary string, count number of substrings that start and end with 1.
- Sub-strings that start and end with one character and have at least one other
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Number of substrings of a string
- Replace two substrings (of a string) with each other
- Count of substrings of a binary string containing K ones
- Number of even substrings in a string of digits
- Program to print all substrings of a given string
- Number of substrings of one string present in other
- Split the string into substrings using delimiter
- Lexicographical concatenation of all substrings of a string
- Sum of all substrings of a string representing a number | Set 1
- Minimum changes to a string to make all substrings distinct
- Number of substrings with odd decimal value in a binary string
- Counting even decimal value substrings in a binary string

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.