Python – Replace all words except the given word
Last Updated :
22 Apr, 2023
Given a string. The task is to replace all the words with ‘?’ except the given word K.
Examples:
Input : test_str = ‘gfg is best for geeks’, K = “gfg”, repl_char = “?”
Output : gfg ? ? ? ?
Explanation : All words except gfg is replaced by ?.
Input : test_str = ‘gfg is best for gfg’, K = “gfg”, repl_char = “?”
Output : gfg ? ? ? gfg
Explanation : All words except gfg is replaced by ?.
Method #1 : Using split() + join() + loop
This is a brute way in which this task can be performed. In this, we perform the task of changing the string to word list using split() and then check for K word, if not found, replace it by the appropriate value. And at last, convert back to the string using join().
Python3
test_str = 'gfg is best for geeks gfg is for cs I love gfg'
print ( "The original string is : " + str (test_str))
K = "gfg"
repl_char = "?"
temp = test_str.split( " " )
for idx in range ( len (temp)):
ele = temp[idx]
if not ele = = K:
temp[idx] = repl_char
res = " " .join(temp)
print ( "The resultant string : " + str (res))
|
Output
The original string is : gfg is best for geeks gfg is for cs I love gfg
The resultant string : gfg ? ? ? ? gfg ? ? ? ? ? gfg
Output:
The original string is : gfg is best for geeks gfg is for cs I love gfg The resultant string : gfg ? ? ? ? gfg ? ? ? ? ? gfg
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension
This is yet another way in which this task can be performed. In this, we iterate for elements and perform the task using one-liner using similar functionality as the above method.
Python3
test_str = 'gfg is best for geeks gfg is for cs I love gfg'
print ( "The original string is : " + str (test_str))
K = "gfg"
repl_char = "?"
res = " " .join(
[repl_char if not ele = = K else ele for ele in test_str.split()])
print ( "The resultant string : " + str (res))
|
Output
The original string is : gfg is best for geeks gfg is for cs I love gfg
The resultant string : gfg ? ? ? ? gfg ? ? ? ? ? gfg
Output:
The original string is : gfg is best for geeks gfg is for cs I love gfg The resultant string : gfg ? ? ? ? gfg ? ? ? ? ? gfg
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using re.sub()
Algorithm for replacing all words except the given word using re approach:
Input: String test_str, word K to keep, replacement character repl_char
Output: Modified string with all words except K replaced with repl_char
Import the re module to work with regular expressions
Print the original string test_str
Initialize K and repl_char
Use the re.sub() function to replace all words in test_str that are not equal to K with repl_char
a. Use the regular expression pattern \b(?!{})\w+\b where \b matches word boundaries, (?!{}) is a negative lookahead that excludes the word K, and \w+ matches one or more word characters
b. Use the format() function to insert K into the pattern
Store the modified string in variable res
Print the resultant string res
This is yet another way in which this task can be performed. In this, we use the regex expression to perform the find and replace task.
Python3
import re
test_str = 'gfg is best for geeks gfg is for cs I love gfg'
print ( "The original string is : " + str (test_str))
K = "gfg"
repl_char = "?"
res = re.sub(r '\b(?!{})\w+\b' . format (K), repl_char, test_str)
print ( "The resultant string : " + str (res))
|
Output
The original string is : gfg is best for geeks gfg is for cs I love gfg
The resultant string : gfg ? ? ? ? gfg ? ? ? ? ? gfg
Time Complexity: The time complexity of the re.sub() function is dependent on the size of the input string, the length of the regular expression pattern, and the number of replacements made. In this case, the regular expression pattern is a simple negative lookahead, so the time complexity of re.sub() is O(n), where n is the length of the input string.
Auxiliary Space Complexity: The auxiliary space complexity of the algorithm is O(n), where n is the length of the input string. This is because the modified string is stored in memory as a new string.
Another Approach:
Algorithm:
- Split the given string into a list of words.
- Create a new list to store the modified words.
- Loop through each word in the list of words.
- If the word is the given word K, append it to the new list.
- Otherwise, append the replacement character to the new list.
- Join the new list of words into a string with spaces between each word.
- Return the modified string.
Python3
def replace_words_except_k(test_str, K, repl_char):
words_list = test_str.split()
new_words_list = []
for word in words_list:
if word = = K:
new_words_list.append(word)
else :
new_words_list.append(repl_char)
modified_str = ' ' .join(new_words_list)
return modified_str
test_str = 'gfg is best for geeks'
K = 'gfg'
repl_char = '?'
output_str = replace_words_except_k(test_str, K, repl_char)
print (output_str)
test_str = 'gfg is best for gfg'
K = 'gfg'
repl_char = '?'
output_str = replace_words_except_k(test_str, K, repl_char)
print (output_str)
|
Output
gfg ? ? ? ?
gfg ? ? ? gfg
Time complexity: O(n), where n is the number of words in the given string.
Auxiliary space: O(n), where n is the number of words in the given string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...