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 the 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 strings of equal length are mapped to different numbers (and equal strings are mapped equally), simply count the number of necessary sub-strings of a certain length.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfDifferentSubstrings(string s, string a,
string b)
{
int ans = 0;
int ls = s.size(), la = a.size(), lb = b.size();
int x[ls] = { 0 }, y[ls] = { 0 };
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;
}
unordered_set<string> hash;
string curr_substr = "" ;
for ( int i = 0; i < ls; i++) {
if (x[i]) {
for ( int j = i; j < ls; j++) {
if (!y[j])
curr_substr += s[j];
if (y[j]) {
curr_substr += s.substr(j, lb);
if (hash.find(curr_substr) == hash.end())
ans++;
hash.insert(curr_substr);
}
}
curr_substr = "" ;
}
}
return ans;
}
int main()
{
string s = "codecppforfood" ;
string begin = "c" ;
string end = "d" ;
cout << numberOfDifferentSubstrings(s, begin, end)
<< endl;
return 0;
}
|
Java
import java.util.HashSet;
class GFG
{
static int numberOfDifferentSubstrings(String s,
char a, char b)
{
int ans = 0 ;
int ls = s.length();
int [] x = new int [ls];
int [] y = new int [ls];
for ( int i = 0 ; i < ls; i++)
{
if (s.charAt(i) == a)
x[i] = 1 ;
if (s.charAt(i) == b)
y[i] = 1 ;
}
HashSet<String> hash = new HashSet<>();
String curr_substr = "" ;
for ( int i = 0 ; i < ls; i++)
{
if (x[i] != 0 )
{
for ( int j = i; j < ls; j++)
{
if (y[j] == 0 )
curr_substr += s.charAt(i);
if (y[j] != 0 )
{
curr_substr += s.charAt(j);
if (!hash.contains(curr_substr))
ans++;
hash.add(curr_substr);
}
}
curr_substr = "" ;
}
}
return ans;
}
public static void main(String[] args)
{
String s = "codecppforfood" ;
char begin = 'c' ;
char end = 'd' ;
System.out.println(
numberOfDifferentSubstrings(s, begin, end));
}
}
|
Python3
def numberOfDifferentSubstrings(s, a, b):
ans = 0
ls = len (s)
la = len (a)
lb = len (b)
x = [ 0 ] * ls
y = [ 0 ] * ls
for i in range (ls):
if (s[i: la + i] = = a):
x[i] = 1
if (s[i: lb + i] = = b):
y[i] = 1
hash = []
curr_substr = ""
for i in range (ls):
if (x[i]):
for j in range ( i, ls):
if ( not y[j]):
curr_substr + = s[j]
if (y[j]):
curr_substr + = s[j: lb + j]
if curr_substr not in hash :
ans + = 1
hash .append(curr_substr)
curr_substr = ""
return ans
if __name__ = = "__main__" :
s = "codecppforfood"
begin = "c"
end = "d"
print (numberOfDifferentSubstrings(s, begin, end))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int numberOfDifferentSubstrings(String s,
char a, char b)
{
int ans = 0;
int ls = s.Length;
int [] x = new int [ls];
int [] y = new int [ls];
for ( int i = 0; i < ls; i++)
{
if (s[i] == a)
x[i] = 1;
if (s[i] == b)
y[i] = 1;
}
HashSet<String> hash = new HashSet<String>();
String curr_substr = "" ;
for ( int i = 0; i < ls; i++)
{
if (x[i] != 0)
{
for ( int j = i; j < ls; j++)
{
if (y[j] == 0)
curr_substr += s[i];
if (y[j] != 0)
{
curr_substr += s[j];
if (!hash.Contains(curr_substr))
ans++;
hash.Add(curr_substr);
}
}
curr_substr = "" ;
}
}
return ans;
}
public static void Main(String[] args)
{
String s = "codecppforfood" ;
char begin = 'c' ;
char end = 'd' ;
Console.WriteLine(
numberOfDifferentSubstrings(s, begin, end));
}
}
|
Javascript
<script>
function numberOfDifferentSubstrings(s,a,b)
{
let ans = 0;
let ls = s.length;
let x = new Array(ls);
let y = new Array(ls);
for (let i=0;i<ls;i++)
{
x[i]=0;
y[i]=0;
}
for (let i = 0; i < ls; i++)
{
if (s[i] == a)
x[i] = 1;
if (s[i] == b)
y[i] = 1;
}
let hash = new Set();
let curr_substr = "" ;
for (let i = 0; i < ls; i++)
{
if (x[i] != 0)
{
for (let j = i; j < ls; j++)
{
if (y[j] == 0)
curr_substr += s[i];
if (y[j] != 0)
{
curr_substr += s[j];
if (!hash.has(curr_substr))
ans++;
hash.add(curr_substr);
}
}
curr_substr = "" ;
}
}
return ans;
}
let s = "codecppforfood" ;
let begin = 'c' ;
let end = 'd' ;
document.write(numberOfDifferentSubstrings(s, begin, end));
</script>
|
Output:
3
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!