Skip to content
Related Articles

Related Articles

Improve Article

Python | Replacing Nth occurrence of multiple characters in a String with the given character

  • Difficulty Level : Easy
  • Last Updated : 14 May, 2021

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 ‘#’ 
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



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




# 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)
Output: 
Ge#ksfor#ee#s

 

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




# 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)
Output: 
Ge#ksfor#ee#s

 

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




# 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)
Output: 
Ge#ksfor#ee#s

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :