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 Lazyroar’, 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
# Python3 code to demonstrate working of # Replace all words not K # Using join() + split() + loop # initializing string test_str = 'gfg is best for Lazyroar gfg is for cs I love gfg' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = "gfg" # initializing repl_char repl_char = "?" # extracting words temp = test_str.split( " " ) for idx in range ( len (temp)): ele = temp[idx] # replace non K with repl_char if not ele = = K: temp[idx] = repl_char # joining result res = " " .join(temp) # printing result print ( "The resultant string : " + str (res)) |
The original string is : gfg is best for Lazyroar gfg is for cs I love gfg The resultant string : gfg ? ? ? ? gfg ? ? ? ? ? gfg
Output:
The original string is : gfg is best for Lazyroar 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
# Python3 code to demonstrate working of # Replace all words not K # Using list comprehension # initializing string test_str = 'gfg is best for Lazyroar gfg is for cs I love gfg' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = "gfg" # initializing repl_char repl_char = "?" # using one-liner to solve this problem res = " " .join( [repl_char if not ele = = K else ele for ele in test_str.split()]) # printing result print ( "The resultant string : " + str (res)) |
The original string is : gfg is best for Lazyroar gfg is for cs I love gfg The resultant string : gfg ? ? ? ? gfg ? ? ? ? ? gfg
Output:
The original string is : gfg is best for Lazyroar 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
# Python3 code to demonstrate working of # Replace all words not K # Using re.sub() # initializing string import re test_str = 'gfg is best for Lazyroar gfg is for cs I love gfg' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = "gfg" # initializing repl_char repl_char = "?" # using one-liner to solve this problem res = re.sub(r '\b(?!{})\w+\b' . format (K), repl_char, test_str) # printing result print ( "The resultant string : " + str (res)) |
The original string is : gfg is best for Lazyroar 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() # Split the given string into a list of words. new_words_list = [] # Create a new empty list to store the modified words. for word in words_list: # Loop through each word in the list of words. if word = = K: # Check if the current word is equal to the given word K. new_words_list.append(word) # If the word is equal to K, append it to the new list. else : new_words_list.append(repl_char) # If the word is not equal to K, append the replacement character to the new list. modified_str = ' ' .join(new_words_list) # Join the new list of words into a string with spaces between each word. return modified_str # Return the modified string. test_str = 'gfg is best for Lazyroar' K = 'gfg' repl_char = '?' output_str = replace_words_except_k(test_str, K, repl_char) print (output_str) # Output: gfg ? ? ? ? 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 ? ? ? ? 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.