Python | Replace characters after K occurrences
Sometimes, while working with Python strings, we can have problem in which we need to perform replace of characters after certain repetitions of characters. This can have application in many domains including day-day and competitive programming.
Method #1 : Using loop + string slicing
This is brute force way in which this problem can be solved. In this, we run a loop on string and keep track of occurrences and perform replace when above K occurrence.
# Python3 code to demonstrate working of # Replace characters after K occurrences# Using loop + string slices # initializing stringtest_str = "geeksforgeeks is best for geeks" # printing original stringprint("The original string is : " + test_str) # initializing K K = 2 # initializing Repl charrepl_char = "*" # Replace characters after K occurrences# Using loop + string slicesfor sub in set(test_str): for idx in [idx for idx in range(len(test_str)) if test_str[idx] == sub][K:]: test_str = test_str[:idx] + repl_char + test_str[idx + 1:] # printing result print("The string after performing replace : " + test_str) |
The original string is : geeksforgeeks is best for geeks The string after performing replace : geeksforg**ks i* b**t*for******
Method #2 : Using join() + count() + enumerate()
This is shorthand by which this task can be performed. In this, we employ count() to check for the count of strings and join() and enumerate() can be used to perform the task of new string construction.
# Python3 code to demonstrate working of # Replace characters after K occurrences# Using join() + count() + enumerate() # initializing stringtest_str = "geeksforgeeks is best for geeks" # printing original stringprint("The original string is : " + test_str) # initializing K K = 2 # initializing Repl charrepl_char = "*" # Replace characters after K occurrences# Using join() + count() + enumerate()res = "".join(chr if test_str.count(chr, 0, idx) < K else repl_char for idx, chr in enumerate(test_str)) # printing result print("The string after performing replace : " + res) |
The original string is : geeksforgeeks is best for geeks The string after performing replace : geeksforg**ks i* b**t*for******


