Given three strings S, oldW and newW. Find all occurrences of the word oldW in S and replace them with word newW.
Example 1:
Input: 
S = "xxforxx xx for xx"
oldW = "xx"
newW = "Geeks"
Output: 
"geeksforgeeks geeks for geeks" 
Explanation: 
Replacing each "xx" with
"Geeks" in S.
Example 2:
Input: 
S = "india is the xx country"
oldW = "xx"
newW = "best"
Output: 
"india is the best country"
Explanation: 
Replacing each "xx" with
"best" in S.
Your Task:  
You dont need to read input or print anything. Complete the function ReplaceAll() which takes S, oldW and newW as input parameters and returns the string S after replacing all oldW with newW.
Expected Time Complexity: O(n^{2})
Expected Auxiliary Space: O(1)
Constraints:
1<= |S| <=1000
1<= |oldW|, |newW| <=|S|

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3
class Solution:
    def replaceAll (ob, S, oldW, newW):
        # code here 

Solutions:
class Solution:

	def replaceAll(ob, S, oldW, newW):
		s = S.replace(oldW, newW)
		return s


class Solution:

	def replaceAll(ob, S, oldW, newW):
		new_string = S.replace(oldW, newW)
		return new_string


class Solution:

	def replaceAll(ob, S, oldW, newW):
		a = S.replace(oldW, newW)
		return a


class Solution:

	def replaceAll(ob, S, oldW, newW):
		X = S.replace(oldW, newW)
		return X


class Solution:

	def replaceAll(ob, S, oldW, newW):
		P = S.replace(oldW, newW)
		return P


class Solution:

	def replaceAll(ob, S, oldW, newW):
		s1 = S.replace(oldW, newW)
		return s1


class Solution:

	def replaceAll(ob, S, oldW, newW):
		S1 = S.replace(oldW, newW)
		return S1


class Solution:

	def replaceAll(ob, S, oldW, newW):
		if oldW in S:
			S = S.replace(oldW, newW)
			return S


class Solution:

	def replaceAll(ob, S, oldW, newW):
		t = S.replace(oldW, newW)
		return t


class Solution:

	def replaceAll(ob, S, oldW, newW):
		x = S.replace(oldW, newW)
		return x


class Solution:

	def replaceAll(ob, s, o, n):
		s = s.replace(o, n)
		return s


class Solution:

	def replaceAll(ob, S, oldW, newW):
		k = S.replace(oldW, newW)
		return k


class Solution:

	def replaceAll(ob, S, oldW, newW):
		oldl = len(oldW)
		newl = len(newW)
		l = list(S)
		i = 0
		while i < len(l):
			if l[i:i + oldl] == list(oldW):
				l[i:i + oldl] = list(newW)
				i += newl - 1
			else:
				i += 1
		return ''.join(l)


class Solution:

	def replaceAll(ob, S, old, new):
		K = S.replace(old, new)
		return K


class Solution:

	def replaceAll(ob, S, oldW, newW):
		res = S.replace(oldW, newW)
		return res


class Solution:

	def replaceAll(ob, S, oldW, newW):
		st = S.replace(oldW, newW)
		return st


import re

class Solution:

	def replaceAll(ob, S, oldW, newW):
		return re.sub(oldW, newW, S)


class Solution:

	def replaceAll(ob, S, oldW, newW):
		X = ''
		i = 0
		while i < len(S):
			if S[i:i + len(oldW)] == oldW:
				X += newW
				i += len(oldW)
			else:
				X += S[i]
				i += 1
		return X


class Solution:

	def replaceAll(ob, S, oldW, newW):
		result = S.replace(oldW, newW)
		return result


class Solution:

	def replaceAll(self, s, oldW, newW):
		new = s.replace(oldW, newW)
		return new


class Solution:

	def replaceAll(ob, S, oldW, newW):
		d = S.replace(oldW, newW)
		return d


class Solution:

	def replaceAll(self, s, oldW, newW):
		new_s = ''
		i = 0
		while i < len(s) - len(oldW):
			word = s[i:i + len(oldW)]
			if word == oldW:
				new_s += newW
				i += len(oldW)
			else:
				new_s += s[i]
				i += 1
		if s[i:] == oldW:
			new_s += newW
		else:
			new_s += s[i:]
		return new_s


class Solution:

	def replaceAll(ob, s, oldW, newW):
		ans = s.replace(oldW, newW)
		return ans


class Solution:

	def replaceAll(ob, S, oldW, newW):
		for i in range(len(S)):
			x = S.replace(oldW, newW)
		return x


class Solution:

	def replaceAll(ob, S, oldW, newW):
		n = S.replace(oldW, newW)
		return n


class Solution:

	def replaceAll(ob, S, o, n):
		return S.replace(o, n)


class Solution:

	def replaceAll(ob, S, oldW, newW):
		h = S.replace(oldW, newW)
		return h


class Solution:

	def replaceAll(ob, S, oldW, newW):
		val = S.replace(oldW, newW)
		return val


class Solution:

	def replaceAll(ob, S, oldW, newW):
		ans = S.replace(oldW, newW)
		return ans


class Solution:

	def replaceAll(ob, S, oldW, newW):
		import re
		return S.replace(oldW, newW)


class Solution:

	def replaceAll(ob, S, oldW, newW):
		words = S.replace(oldW, newW)
		return words


class Solution:

	def replaceAll(ob, S, oldW, newW):
		str = S.replace(oldW, newW)
		return str


class Solution:

	def replaceAll(ob, S, oldW, newW):
		i = 0
		ans = ''
		j = len(oldW)
		while i + j <= len(S):
			a = S[i:i + j]
			if a == oldW:
				ans += newW
				i += j
			else:
				ans += S[i]
				i += 1
		for i in S[i:]:
			ans += i
		return ans


class Solution:

	def replaceAll(ob, S, oldW, newW):
		s = str(S)
		o = str(oldW)
		n = str(newW)
		lst = s.split(o)
		n_S = n.join(lst)
		return n_S


class Solution:

	def replaceAll(ob, S, oldW, newW):
		ls = S.split(oldW)
		S2 = ''
		for i in range(0, len(ls)):
			if i == len(ls) - 1:
				S2 = S2 + ls[i]
			else:
				S2 = S2 + ls[i] + newW
		return S2