Count occurrences of a sub-string with one variable character
Last Updated :
24 Mar, 2023
Given two strings a and b, and an integer k which is the index in b at which the character can be changed to any other character, the task is to check if b is a sub-string in a and print out how many times b occurs in a in total after replacing the b[k] with every possible lowercase character of English alphabet.
Examples:
Input: a = “geeks”, b = “ee”, k = 1
Output: 1 Replace b[1] with ‘k’ and “ek” is a sub-string in “geeks” “ee” is also a sub-string in “geeks” Hence the total count is 2
Input: a = “dogdog”, b = “dop”, k = 2
Output: 2 Replace b[2] with ‘g’, “dog” is a sub-string in “dogdog” which appears twice.
Approach: Make a list of all possible versions of the string b by iterating through all the lowercase letters and replacing the kth i.e. b[k] character in b with the current character. Then count the number of occurrence of the new string b in the original string a and store it in a variable count. After all the lowercase characters are used, print the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_(string a, string var)
{
int c = 0;
int l = var.length();
for ( int i = 0; i < a.length() - l; i++)
if (a.substr(i, l) == var)
c++;
return c;
}
int countOccurrence(string a, string b, int k)
{
vector<string> x;
for ( int i = 0; i < 26; i++) {
string temp = b;
temp[k] = 'a' + i;
x.push_back(temp);
}
int count = 0;
for ( auto var : x) {
if (a.find(var) != string::npos)
count += count_(a, var);
}
return count;
}
int main()
{
string a = "geeks" , b = "ee" ;
int k = 1;
cout << countOccurrence(a, b, k) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int count_(String a, String var) {
int c = 0 ;
int l = var.length();
for ( int i = 0 ; i < a.length() - l; i++) {
if (a.substring(i, i + l).equals(var)) {
c++;
}
}
return c;
}
public static int countOccurrence(String a, String b, int k) {
List<String> x = new ArrayList<>();
for ( int i = 0 ; i < 26 ; i++) {
char [] temp = b.toCharArray();
temp[k] = ( char )( 'a' + i);
x.add(String.valueOf(temp));
}
int count = 0 ;
for (String var : x) {
if (a.indexOf(var) != - 1 ) {
count += count_(a, var);
}
}
return count;
}
public static void main(String[] args) {
String a = "geeks" ;
String b = "ee" ;
int k = 1 ;
System.out.println(countOccurrence(a, b, k));
}
}
|
Python3
import string
def countOccurrence(a, b, k):
x = []
for i in range ( 26 ):
x.append(b[ 0 :k] + string.ascii_lowercase[i] + b[k + 1 :])
count = 0
for var in x:
if var in a:
count + = a.count(var)
return count
a, b = "geeks" , "ee"
k = 1
print (countOccurrence(a, b, k))
|
Javascript
function countOccurrence(a, b, k) {
let x = [];
for (let i = 0; i < 26; i++) {
x.push(b.substring(0, k) + String.fromCharCode(97 + i) + b.substring(k + 1));
}
let count = 0;
for (let var1 of x) {
if (a.includes(var1)) {
count += a.split(var1).length - 1;
}
}
return count;
}
let a = "geeks" ;
let b = "ee" ;
let k = 1;
console.log(countOccurrence(a, b, k));
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int count_( string a, string var )
{
int c = 0;
int l = var .Length;
for ( int i = 0; i < a.Length - l; i++)
if (a.Substring(i, l) == var )
c++;
return c;
}
static int countOccurrence( string a, string b, int k)
{
List< string > x = new List< string >();
for ( int i = 0; i < 26; i++) {
string temp = b;
temp = temp.Remove(k, 1).Insert(
k, Convert.ToString(( char )( 'a' + i)));
x.Add(temp);
}
int count = 0;
foreach ( var var in x)
{
if (a.Contains( var ))
count += count_(a, var );
}
return count;
}
static void Main()
{
string a = "geeks" , b = "ee" ;
int k = 1;
Console.WriteLine(countOccurrence(a, b, k));
}
}
|
Share your thoughts in the comments
Please Login to comment...