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 2Input: 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:
# 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 = 1print(countOccurrence(a, b, k)) |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Internal static variable vs. External static variable with Examples in C
- Python | Get the starting index for all occurrences of given substring
- Python | All occurrences of substring in string
- Python - All occurrences of Substring from the list of strings
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Python | Deleting all occurrences of character
- Python - Non-Overlapping occurrences of N Repeated K character
- Check if a binary string has two consecutive occurrences of one everywhere
- Count of strings possible by replacing two consecutive same character with new character
- Count of occurrences of a "1(0+)1" pattern in a string
- Count occurrences of a word in string
- Count Occurrences of Anagrams
- Count occurrences of a string that can be constructed from another given string
- Python | Count occurrences of each word in given text file (Using dictionary)
- Python | Count String occurrences in mixed list
- Count of occurrences of each prefix in a string using modified KMP algorithm
- Replace every character of string by character whose ASCII value is K times more than it
- Python | Insert character after every character pair
- Match Expression where a single special character in pattern can match one or more characters
- NFA to accept strings that has atleast one character occurring in a multiple of 3
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.
Improved By : nidhi_biet

