Count occurrences of a sub-string with one variable character Last Updated : 24 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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++ // c++ implementation of above approach #include <bits/stdc++.h> using namespace std; // function to implement count 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; } // Function to return the count of occurrences int countOccurrence(string a, string b, int k) { // Generate all possible substrings to // be searched vector<string> x; for (int i = 0; i < 26; i++) { string temp = b; temp[k] = 'a' + i; x.push_back(temp); } // Now search every substring 'a' and // increment count 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; } // This code is contributed by Abhijeet Kumar(abhijeet19403) Java // Java program for the above approach import java.util.*; public class Main { // function to implement count 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; } // Function to return the count of occurrences public static int countOccurrence(String a, String b, int k) { // Generate all possible substrings to be searched 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)); } // Now search every substring 'a' and increment count 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)); } } // This code is contributed by Prince Kumar Python3 # Python3 implementation of the approach import string # Function to return the count of occurrences def countOccurrence(a, b, k): # Generate all possible substrings to # be searched x = [] for i in range(26): x.append(b[0:k] + string.ascii_lowercase[i] + b[k + 1:]) # Now search every substring 'a' and # increment count count = 0 for var in x: if var in a: count += a.count(var) return count # Driver code a, b = "geeks", "ee" k = 1 print(countOccurrence(a, b, k)) JavaScript // JavaScript implementation of the approach // Function to return the count of occurrences function countOccurrence(a, b, k) { let x = []; // Generate all possible substrings to be searched for (let i = 0; i < 26; i++) { x.push(b.substring(0, k) + String.fromCharCode(97 + i) + b.substring(k + 1)); } // Now search every substring 'a' and increment count let count = 0; for (let var1 of x) { if (a.includes(var1)) { count += a.split(var1).length - 1; } } return count; } // Driver code let a = "geeks"; let b = "ee"; let k = 1; console.log(countOccurrence(a, b, k)); C# // c# implementation of above approach using System; using System.Collections.Generic; class GFG { // function to implement count 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; } // Function to return the count of occurrences static int countOccurrence(string a, string b, int k) { // Generate all possible substrings to // be searched 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); } // Now search every substring 'a' and // increment count 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)); } } Output:2 Comment More infoAdvertise with us Next Article Count occurrences of a sub-string with one variable character dkp1903 Follow Improve Article Tags : Pattern Searching Technical Scripter Python DSA Practice Tags : Pattern Searchingpython Similar Reads Count occurrences of a character in string in Python We are given a string, and our task is to count how many times a specific character appears in it using Python. This can be done using methods like .count(), loops, or collections.Counter. For example, in the string "banana", using "banana".count('a') will return 3 since the letter 'a' appears three 2 min read Count occurrences of a character in a repeated string Given an integer N and a lowercase string. The string is repeated infinitely. The task is to find the No. of occurrences of a given character x in first N letters.Examples: Input : N = 10 str = "abcac"Output : 4Explanation: "abcacabcac" is the substring from the infinitely repeated string. In first 8 min read Count Occurrences of a Given Character in a String Given a string S and a character 'c', the task is to count the occurrence of the given character in the string.Examples: Input : S = "geeksforgeeks" and c = 'e'Output : 4Explanation: 'e' appears four times in str.Input : S = "abccdefgaa" and c = 'a' Output : 3Explanation: 'a' appears three times in 6 min read Count of substrings with the frequency of at most one character as Odd Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times. Example: Input: S = "aba"Output: 4Explanation: The valid substrings are "a", "b", "a", and "aba". Therefore, the total number of required 7 min read Count of all unique substrings with non-repeating characters Given a string str consisting of lowercase characters, the task is to find the total number of unique substrings with non-repeating characters. Examples: Input: str = "abba" Output: 4 Explanation: There are 4 unique substrings. They are: "a", "ab", "b", "ba". Input: str = "acbacbacaa" Output: 10 App 6 min read Count of substrings of a given Binary string with all characters same Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same. Examples: Input: str = â011âOutput: 4Explanation: Three sub-strings are "1", "1", "11" which have only 1 in them, and one substring is there 10 min read Count substrings with each character occurring at most k times Given a string S. Count number of substrings in which each character occurs at most k times. Assume that the string consists of only lowercase English alphabets. Examples: Input : S = ab k = 1 Output : 3 All the substrings a, b, ab have individual character count less than 1. Input : S = aaabb k = 2 15+ min read Count all sub-strings with weight of characters atmost K Given a string P consisting of small English letters and a string Q consisting of weight of all characters of English alphabet such that for all 'i', 0 ? Q[i] ? 9. The task is to find the total numbers of unique substring with sum of weights atmost K.Examples: Input: P = "ababab", Q = "1234567891234 7 min read Count of sub-strings that do not consist of the given character Given a string str and a character c. The task is to find the number of sub-strings that do not consist of the character c. Examples: Input: str = "baa", c = 'b' Output: 3 The sub-strings are "a", "a" and "aa" Input: str = "ababaa", C = 'b' Output: 5 Approach: Initially take a counter that counts th 12 min read Count substrings with same first and last characters Given a string s consisting of lowercase characters, the task is to find the count of all substrings that start and end with the same character.Examples : Input : s = "abcab"Output : 7Explanation: The substrings are "a", "abca", "b", "bcab", "c", "a", "b".Input : s = "aba"Output : 4Explanation: The 7 min read Like