给定一个字符串S 、一个字符数组ch[] 、一个数字N和一个替换字符,任务是用给定的替换字符替换字符串字符数组ch[]中每个字符的第 N 次出现。
Input: S = “GeeksforGeeks”, ch[] = {‘G’, ‘e’, ‘k’}, N = 2, replacing_character = ‘#’
Output: Ge#ksfor#ee#s
Explanation:
In the given string S, the second occurrence of the ‘G’, ‘e’, ‘K’ is replaced with ‘#’
Input: S = abcdeahu, ch[] = {‘a’, ‘d’, ‘u’}, N = 1, replacing_character = ‘#’
Output: #bc#eah#
Explanation:
In the given string S, the first occurrence of the ‘a’, ‘d’, ‘u’ is replaced with ‘#’
方法 1:天真的方法
在这种方法中,一般的想法是将每个字符的第 N 个出现索引存储在另一个数组中,并用给定的另一个字符替换它们。
下面是上述方法的实现。
Python
# Python implementation to replace nth
# occurrence of the every character
# in a string
# Function to replace the Nth occurrence
# of the character of string
def replacer(initial_string, ch,
replacing_character, occurrence):
# breaking a string into it's
# every single character in list
lst1 = list(initial_string)
lst2 = list(ch)
# List to store the indexes in which
# it is replaced with the
# replacing_character
lst3 = []
# Loop to find the Nth occurrence of
# given characters in the string
for i in lst2:
if(lst1.count(i)>= occurrence):
count = 0
for j in range(0, len(initial_string)):
if(i == lst1[j]):
count+= 1
if(count == occurrence):
lst3.append(j)
for i in lst3:
# Replacing that particular index
# with the requested character
lst1[i] = replacing_character
print(''.join(lst1))
# Driver Code:
if __name__ == '__main__':
initial_string = 'GeeksforGeeks'
ch = ['G', 'e', 'k']
occurrence = 2
replacing_character = '#'
replacer(initial_string, ch,
replacing_character, occurrence)
Python
# Python implementation to replace nth
# occurrence of the every character
# in a string using find() function
# Function to replace the Nth occurrence
# of the character of string
def replacer(initial_string, ch,
replacing_character, occurrence):
# breaking a string into
# it's every single character
lst1 = list(initial_string)
lst2 = list(ch)
# Loop to find the occurrence
# of the character in the string
# and replace it with the given
# replacing_character
for i in lst2:
sub_string = i
val = -1
for i in range(0, occurrence):
val = initial_string.find(sub_string,
val + 1)
lst1[val] = replacing_character
print(''.join(lst1))
# Driver Code:
if __name__ == '__main__':
initial_string = 'GeeksforGeeks'
ch = ['G', 'e', 'k']
occurrence = 2
replacing_character = '#'
replacer(initial_string, ch,
replacing_character, occurrence)
Python
# Python implementation to replace nth
# occurrence of the every character
# in a string
# Function to replace the Nth occurrence
# of the character of string
def replacer(initial_string, ch,
replacing_character, occurrence):
# breaking a string into
# it's every single character
lst1 = list(initial_string)
lst2 = list(ch)
# Loop to find the occurrence
# of the character in the string
# and replace it with the given
# replacing_character
for j in lst2:
sub_string = j
checklist = [i for i in range(0, len(initial_string))
if initial_string[i:].startswith(sub_string)]
if len(checklist)>= occurrence:
lst1[checklist[occurrence-1]] = replacing_character
print(''.join(lst1))
# Driver Code:
if __name__ == '__main__':
initial_string = 'GeeksforGeeks'
ch = ['G', 'e', 'k']
occurrence = 2
replacing_character = '#'
replacer(initial_string, ch,
replacing_character, occurrence)
Ge#ksfor#ee#s
方法二:使用 find() 方法
在这种方法中,想法是使用 find()函数查找给定字符串S 中出现的第 N 个字符,并将其替换为另一个给定字符。
下面是上述方法的实现。
Python
# Python implementation to replace nth
# occurrence of the every character
# in a string using find() function
# Function to replace the Nth occurrence
# of the character of string
def replacer(initial_string, ch,
replacing_character, occurrence):
# breaking a string into
# it's every single character
lst1 = list(initial_string)
lst2 = list(ch)
# Loop to find the occurrence
# of the character in the string
# and replace it with the given
# replacing_character
for i in lst2:
sub_string = i
val = -1
for i in range(0, occurrence):
val = initial_string.find(sub_string,
val + 1)
lst1[val] = replacing_character
print(''.join(lst1))
# Driver Code:
if __name__ == '__main__':
initial_string = 'GeeksforGeeks'
ch = ['G', 'e', 'k']
occurrence = 2
replacing_character = '#'
replacer(initial_string, ch,
replacing_character, occurrence)
Ge#ksfor#ee#s
方法三:使用startswith()方法
在这种方法中,这个想法是使用Python的startswith()函数来找到字符的索引,其中的字符的发生是等于给定第N次出现,然后用给定的替换字符替换。
下面是上述方法的实现。
Python
# Python implementation to replace nth
# occurrence of the every character
# in a string
# Function to replace the Nth occurrence
# of the character of string
def replacer(initial_string, ch,
replacing_character, occurrence):
# breaking a string into
# it's every single character
lst1 = list(initial_string)
lst2 = list(ch)
# Loop to find the occurrence
# of the character in the string
# and replace it with the given
# replacing_character
for j in lst2:
sub_string = j
checklist = [i for i in range(0, len(initial_string))
if initial_string[i:].startswith(sub_string)]
if len(checklist)>= occurrence:
lst1[checklist[occurrence-1]] = replacing_character
print(''.join(lst1))
# Driver Code:
if __name__ == '__main__':
initial_string = 'GeeksforGeeks'
ch = ['G', 'e', 'k']
occurrence = 2
replacing_character = '#'
replacer(initial_string, ch,
replacing_character, occurrence)
Ge#ksfor#ee#s
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live