给定两个字符串A和B,以及整数k是在该字符可以更改为其他任何字符在B指标,任务是检查是否b是子字符串中并打印出多少次数b发生在总共有英文字母的每一个可能的小写字符替换b [k]的后。
例子:
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.
方法:通过所有的小写字母迭代,并与当前字符替换B中的第k个即B [K]字符使字符串B的所有可能的版本的列表。
再算上在原新的字符串B的出现次数,并将其存储在一个变盘点。使用所有小写字符后,输出count 。
下面是上述方法的实现:
# 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))
输出:
2