Python | Replacing Nth occurrence of multiple characters in a String with the given character
Given a String S, a character array ch[], a number N and a replacing character, the task is to replace every Nth occurrence of each character of the character array ch[] in the string with the given replacing character.
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 ‘#’
Method 1: Naive approach
In this approach, the general idea is to store every Nth Occurrence index of every character in another array and replacing them with the given another character.
Below is the implementation of the above approach.
Python
def replacer(initial_string, ch,
replacing_character, occurrence):
lst1 = list (initial_string)
lst2 = list (ch)
lst3 = []
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:
lst1[i] = replacing_character
print (''.join(lst1))
if __name__ = = '__main__' :
initial_string = 'GeeksforGeeks'
ch = [ 'G' , 'e' , 'k' ]
occurrence = 2
replacing_character = '#'
replacer(initial_string, ch,
replacing_character, occurrence)
|
Time complexity: O(n^2), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.
Method 2: Using find() method
In this approach, the idea is to use the find() function to find the Nth occurrence of the character in the given string S and replace it with another giver character.
Below is the implementation of the above approach.
Python
def replacer(initial_string, ch,
replacing_character, occurrence):
lst1 = list (initial_string)
lst2 = list (ch)
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))
if __name__ = = '__main__' :
initial_string = 'GeeksforGeeks'
ch = [ 'G' , 'e' , 'k' ]
occurrence = 2
replacing_character = '#'
replacer(initial_string, ch,
replacing_character, occurrence)
|
Time complexity: O(n*m), where n is the length of the initial_string and m is the length of the list ch.
Auxiliary Space: O(n), where n is the length of the initial_string.
Method 3: Using startswith() method
In this approach, the idea is to use the startswith() function of the python to find the index of the character where the occurrence of the character is equal to the given Nth occurrence and then replace with the given replacing character.
Below is the implementation of the above approach.
Python
def replacer(initial_string, ch,
replacing_character, occurrence):
lst1 = list (initial_string)
lst2 = list (ch)
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))
if __name__ = = '__main__' :
initial_string = 'GeeksforGeeks'
ch = [ 'G' , 'e' , 'k' ]
occurrence = 2
replacing_character = '#'
replacer(initial_string, ch,
replacing_character, occurrence)
|
Last Updated :
08 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...