Given a List. The task is to replace all the characters of the list with N except the given character.
Input : test_list = ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T'], repl_chr = '*', ret_chr = 'G'
Output : ['G', '*', 'G', '*', '*', '*', '*', '*', '*']
Explanation : All characters except G replaced by *
Input : test_list = ['G', 'F', 'G', 'B', 'E', 'S', 'T'], repl_chr = '*', ret_chr = 'G'
Output : ['G', '*', 'G', '*', '*', '*', '*']
Explanation : All characters except G replaced by *
In this, we perform the task of iteration using list comprehension, and replacements are taken care of using conditional operators.
OutputThe original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement : ['G', '$', 'G', '$', '$', '$', '$', '$', '$']
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1) additional space is not needed.
In this, we use map() and lambda function to extend the logic to each element of the list.
OutputThe original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement : ['G', '$', 'G', '$', '$', '$', '$', '$', '$']
OutputThe original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement : ['G', '$', 'G', '$', '$', '$', '$', '$', '$']
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, since we are creating a new list to store the modified characters.
OutputThe original list: ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement: ['G', '$', 'G', '$', '$', '$', '$', '$', '$']
Time complexity: The time complexity of this method is O(n), where n is the length of the input list test_list.
Auxiliary space: The auxiliary space complexity is O(n), where n is the length of the input list.