Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Kth word replace in String

Python – Kth word replace in String

Sometimes, while working with String list, we can have a problem in which we need to replace the most Kth word of string. This problem has many applications in the web development domain. Let’s discuss a way in which this problem can be solved. 

Method 1: Using split() + join() This is way in which we can perform this task. In this, we break elements into parts and then return the Kth value and perform the addition of new element using join(). 

Python3




# Python3 code to demonstrate working of
# Kth word replace in String
# using split() + join()
 
# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# initializing K
K = 1
 
# Kth word replace in String
# using split() + join()
temp = test_str.split(' ')
res = " ".join(temp[: K]  + [rep_str] + temp[K + 1 :])
 
# printing result
print("The String after performing replace : " + res)


Output : 

The original string is : GFG is good
The String after performing replace : GFG best good

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n + m), where n and m are the length of original string and replace string respectively.

Method 2: Using split() and join() methods without slicing

Python3




# Python3 code to demonstrate working of
# Kth word replace in String
# using split() + join()
 
# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# initializing K
K = 1
 
# Kth word replace in String
# using split() + join()
temp = test_str.split(' ')
temp[K]=rep_str
res=" ".join(temp)
 
# printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG best good

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n + m), where n and m are the length of original string and replace string respectively.

Method #3: Using re.sub() and re.findall()

Python3




import re
 
# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# initializing K
K = 1
 
# Kth word replace in String
# using re.sub() and re.findall()
temp = re.findall(r'\b\w+\b', test_str)
temp[K] = rep_str
res = re.sub(r'\b\w+\b', lambda x: temp.pop(0), test_str)
 
# printing result
print("The String after performing replace : " + res)
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string is : GFG is good
The String after performing replace : GFG best good

This method uses regular expression to find all words in the string and replace the Kth word with the new one using re.sub() function.

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n + m), where n and m are the length of original string and replace string respectively.

Method#4: Using a loop and string concatenation

Python3




# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# initializing K
K = 1
 
# Kth word replace in String
# using a loop and string concatenation
words = test_str.split()
res = ''
for i, word in enumerate(words):
    if i == K:
        res += rep_str + ' '
    else:
        res += word + ' '
 
# remove trailing space
res = res[:-1]
 
# printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG best good

Step-by-step algorithm for implementing the approach

  1. Initialize the given string test_str, the replacement string rep_str, and the index K.
  2. Split the test_str using the split() function to create a list of words.
  3. Loop over each word in the list and check if the current index is equal to K.
  4. If the current index is equal to K, add the rep_str to the result string res, followed by a space.
  5. If the current index is not equal to K, add the current word to the result string res, followed by a space.
  6. Remove the trailing space from the result string res.
  7. Print the final result string res.

Time complexity: The time complexity of the approach depends on the size of the given string test_str and the number of words in it. The split() function takes O(n) time to split the string, where n is the length of the test_str. The loop over each word takes O(m) time, where m is the number of words in the test_str. The if condition inside the loop and concatenation operations take constant time, i.e., O(1). Therefore, the overall time complexity is O(n + m).

Auxiliary space complexity: The auxiliary space complexity of the approach is O(n), where n is the length of the test_str. This is because we split the test_str using the split() function and create a new string res to store the result. The space required by these data structures is proportional to the length of the test_str.

Method 5: using the string method replace() and list indexing.

Steps:

  1. Initialize the string, test_str.
  2. Print the original string.
  3. Initialize the replace string, rep_str.
  4. Initialize the value of K.
  5. Split the string into a list of words using the split() method and assign it to variable words.
  6. Replace the Kth word in the list words with rep_str.
  7. Join the words back into a string using the join() method with a space separator and assign it to a variable res.
  8. Print the string after performing the replacement.

Python3




# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# initializing K
K = 1
 
# Kth word replace in String using replace()
# method and list indexing
words = test_str.split()
words[K] = rep_str
res = ' '.join(words)
 
# printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG best good

The time complexity of this method is O(n), where n is the number of words in the string test_str. 

The auxiliary space required is O(n), since we create a list of words from the string.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments