Given a string, eliminate all “b” and “ac” in the string, replace them in-place and iterate over the string once.
Example 1:
Input: str = "aacbacc"
Output: ac
Explanation: Iterate once and remove
occurrence of "b" and "ac"
Example 2:
Input: str = "aacb"
Output: a
Explanation: Iterate once and remove
occurrence of "b" and "ac"
Your task:
Your task is to complete the function stringFilter() which takes a single string as input and modify the string as mentioned above. You need not take any input or print anything.
 
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(1)
Constraints:
1 <= |S| <= 10^{5}
String contains lower case alphabets

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def stringFilter(self, str):
        # code here

Solutions:
class Solution:

	def stringFilter(self, str):
		str = str.replace('ac', '')
		str = str.replace('b', '')
		return str


class Solution:

	def stringFilter(self, str):
		res = ''
		i = 0
		while i < len(str):
			if str[i] == 'a':
				if i + 1 < len(str):
					if str[i + 1] != 'c':
						res += str[i]
					else:
						i += 1
				else:
					res += str[i]
			elif str[i] != 'b':
				res += str[i]
			i += 1
		return res


class Solution:

	def stringFilter(self, str):
		result = ''
		for x in range(len(str)):
			if str[x] == 'b':
				continue
			elif str[x] == 'c' and str[x - 1] == 'a':
				result = result[:len(result) - 1]
			else:
				result += str[x]
		return result


class Solution:

	def stringFilter(self, str):
		s2 = ''
		s1 = s.replace('ac', ' ').replace('b', ' ')
		for i in range(len(s1)):
			if s1[i] != ' ':
				s2 = s2 + s1[i]
		return s2


class Solution:

	def stringFilter(self, s):
		s = s.replace('ac', '')
		s = s.replace('b', '')
		return s


class Solution:

	def stringFilter(self, str):
		ans = ''
		i = 0
		while i < len(str):
			if i < len(str) - 1 and (str[i] == 'b' or str[i] + str[i + 1] == 'ac'):
				if str[i] == 'b':
					i += 1
				else:
					i += 2
			else:
				if s[i] != 'b':
					ans += str[i]
				i += 1
		return ans


class Solution:

	def stringFilter(self, str):
		x = str.replace('ac', '')
		y = x.replace('b', '')
		return y


class Solution:

	def stringFilter(self, str):
		if len(str) == 0:
			return str
		ans = ''
		i = 0
		while i < len(str) - 1:
			c = str[i]
			n = str[i + 1]
			if c == 'a' and n == 'c':
				i = i + 2
				continue
			elif c != 'b':
				ans += c
			i += 1
		if i == len(str) - 1 and str[i] != 'b':
			ans += str[i]
		return ans


class Solution:

	def stringFilter(self, str):
		if len(str) == 0:
			return str
		ans = str[0] if str[0] != 'b' else ''
		for i in range(1, len(str)):
			prev = str[i - 1]
			curr = str[i]
			if curr == 'b':
				continue
			elif curr == 'c' and prev == 'a':
				ans = ans[:-1]
			else:
				ans = ans + curr
		return ans


class Solution:

	def stringFilter(self, str1):
		str1 = str1.replace('ac', '')
		str1 = str1.replace('b', '')
		return str1


class Solution:

	def stringFilter(self, str):
		chars = list(str)
		(idx, i) = (0, 0)
		while i < len(chars):
			if chars[i] == 'b':
				i += 1
			elif chars[i] == 'a':
				if i + 1 < len(chars) and chars[i + 1] == 'c':
					i += 2
				else:
					chars[idx] = 'a'
					i += 1
					idx += 1
			else:
				chars[idx] = chars[i]
				i += 1
				idx += 1
		del chars[idx:]
		chars = ''.join(chars)
		return chars


class Solution:

	def stringFilter(self, str):
		res = ''
		n = len(str)
		for i in range(0, n):
			if str[i] != 'b' and str[i:i + 2] != 'ac' and (str[i - 1:i + 1] != 'ac'):
				res += str[i]
		return res


class Solution:

	def stringFilter(self, str):
		s = ''
		i = 0
		while i < len(str):
			if str[i] == 'b':
				i = i + 1
			elif i + 1 < len(str) and str[i] == 'a' and (str[i + 1] == 'c'):
				i += 2
			else:
				s = s + str[i]
				i = i + 1
		return s


class Solution:

	def stringFilter(self, S):
		x = S.replace('ac', '')
		x = x.replace('b', '')
		return x


class Solution:

	def stringFilter(self, str):
		l = len(str)
		for i in range(l - 1):
			if str[i] == 'b':
				str = str[:i] + '.' + str[i + 1:]
				continue
			if str[i] == 'a' and str[i + 1] == 'c':
				str = str[:i] + '..' + str[i + 2:]
		str = str.replace('.', '')
		if 'b' in str:
			str = str[:len(str) - 1]
		return str


class Solution:

	def stringFilter(self, s):
		li = list(s)
		i = 0
		while i < len(li):
			if i + 1 < len(li):
				if li[i] == 'a' and li[i + 1] == 'c':
					li.pop(i)
					li.pop(i)
					continue
			if li[i] == 'b':
				li.pop(i)
				continue
			i += 1
		return ''.join(li)


class Solution:

	def stringFilter(self, str):
		pop = []
		for i in range(len(str) - 1):
			if str[i] == 'b':
				pop.append(i)
			elif str[i] + str[i + 1] == 'ac':
				pop.append(i)
				pop.append(i + 1)
		if str[-1] == 'b':
			str = str[:-1]
		pop.reverse()
		l = [*str]
		for item in pop:
			l.pop(item)
		return ''.join(l)


class Solution:

	def stringFilter(self, str):
		size = len(str)
		str = self.toList(str)
		(i, j) = (0, 0)
		while j < size:
			if str[j] == 'b':
				j += 1
			elif j + 1 < size and str[j] == 'a' and (str[j + 1] == 'c'):
				j += 2
			else:
				str[i] = str[j]
				i += 1
				j += 1
		return self.toString(str[:i])

	def toString(self, l):
		return ''.join(l)

	def toList(self, string):
		l = []
		for x in string:
			l.append(x)
		return l


class Solution:

	def stringFilter(self, str):
		a = str.split('ac')
		x = ''.join(a)
		b = x.split('b')
		y = ''.join(b)
		return y


class Solution:

	def stringFilter(self, str):
		length = len(str)
		fstr = ''
		skip = 0
		for i in range(length):
			if skip == 1:
				skip = 0
				continue
			if str[i] == 'b':
				skip = 0
				continue
			elif i + 1 < length and str[i] + str[i + 1] == 'ac':
				skip = 1
				continue
			else:
				skip = 0
				fstr += str[i]
		return fstr


class Solution:

	def stringFilter(self, string):
		result = ''
		skip_next = False
		for (i, c) in enumerate(string):
			if skip_next:
				skip_next = False
				continue
			if c == 'b':
				continue
			elif c == 'a' and i < len(string) - 1 and (string[i + 1] == 'c'):
				skip_next = True
				continue
			result += c
		return result


class Solution:

	def stringFilter(self, str):
		a = str.replace('b', '1')
		b = a.replace('ac', '0')
		s = ''
		for i in b:
			if i.isdigit():
				pass
			else:
				s += i
		return s


class Solution:

	def stringFilter(self, str):
		s = str.split('ac')
		st = ''
		for i in s:
			st = st + i
		s = st.split('b')
		st = ''
		for i in s:
			st = st + i
		return st


class Solution:

	def stringFilter(self, str):
		l = []
		last = ''
		for i in str:
			if last == 'a' and i == 'c':
				last = i
				l.pop()
			elif i == 'b':
				last = i
				continue
			else:
				last = i
				l.append(i)
		return ''.join(l)


class Solution:

	def stringFilter(self, strg):
		i = 0
		n = len(strg)
		news = ''
		while i < n:
			if strg[i] == 'b':
				pass
			elif strg[i] == 'a':
				if i + 1 < n and strg[i + 1] == 'c':
					i += 1
				else:
					news += 'a'
			else:
				news += strg[i]
			i += 1
		return news


class Solution:

	def stringFilter(self, str):
		s = str.split('ac')
		s = ''.join(s)
		s = s.split('b')
		s = ''.join(s)
		return s


class Solution:

	def stringFilter(self, s):
		res = ''
		j = 0
		while j < len(s):
			if s[j] == 'b':
				j += 1
			elif j + 1 < len(s) and s[j] == 'a' and (s[j + 1] == 'c'):
				j += 2
			else:
				res += s[j]
				j += 1
		return res


class Solution:

	def stringFilter(self, str):
		import re
		return re.sub('b|ac', '', str)


class Solution:

	def stringFilter(self, str):
		l = []
		i = 0
		while i < len(str):
			if i <= len(str) - 2 and str[i] == 'a' and (str[i + 1] == 'c'):
				i += 2
			elif str[i] == 'b':
				i += 1
			else:
				l.append(str[i])
				i += 1
		temp = ''
		for i in l:
			temp += i
		return temp


class Solution:

	def stringFilter(self, str):
		arr = []
		result = ''
		for x in range(len(str)):
			if str[x] == 'b':
				continue
			elif str[x] == 'c' and str[x - 1] == 'a':
				arr.pop()
			else:
				arr.append(str[x])
		result = result.join(arr)
		return result


class Solution:

	def stringFilter(self, str):
		if 'ac' in str:
			str = str.replace('ac', '')
		if 'b' in str:
			str = str.replace('b', '')
		return str