Given a string S. For each index i(1<=i<=N-1), erase it if s[i] is equal to s[i-1] in the string.
Example 1:
Input:
S = aabb
Output:  ab 
Explanation: 'a' at 2nd position is
appearing 2nd time consecutively.
Similiar explanation for b at
4th position.
Example 2:
Input:
S = aabaa
Output:  aba
Explanation: 'a' at 2nd position is
appearing 2nd time consecutively.
'a' at fifth position is appearing
2nd time consecutively.
 
Your Task:
You dont need to read input or print anything. Complete the function removeConsecutiveCharacter() which accepts a string as input parameter and returns modified string. 
 
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
 
Constraints:
1<=|S|<=10^{5}
All characters are lowercase alphabets.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def removeConsecutiveCharacter(self, S):

        # code here

Solutions:
class Solution:

	def removeConsecutiveCharacter(self, S):
		l = ''
		l = l + S[0]
		for i in range(1, len(S)):
			if S[i] != S[i - 1]:
				l += S[i]
		return l


class Solution:

	def removeConsecutiveCharacter(self, S):
		st = []
		for i in S:
			if len(st) > 0 and i == st[-1]:
				st.pop()
			st.append(i)
		return ''.join(st)


import itertools

class Solution:

	def removeConsecutiveCharacter(self, s):
		d = itertools.groupby(s)
		l = []
		for (i, j) in d:
			l.append(list(j))
		s = ''
		for i in l:
			for j in range(1):
				s = s + i[j]
		return s


class Solution:

	def removeConsecutiveCharacter(self, S):
		l = []
		if len(s) > 0:
			l.append(s[0])
		for i in range(1, len(s)):
			if s[i] != s[i - 1]:
				l.append(s[i])
		return ''.join(l)


class Solution:

	def removeConsecutiveCharacter(self, S):
		new = ''
		prev = ''
		for x in S:
			if len(new) == 0:
				new += x
				prev = x
			if x == prev:
				continue
			else:
				new += x
				prev = x
		return new


class Solution:

	def removeConsecutiveCharacter(self, S: str):
		output = ''
		for c in range(len(S) - 1):
			if S[c] != S[c + 1]:
				output += S[c]
		output += S[-1]
		return output


class Solution:

	def removeConsecutiveCharacter(self, S):
		i = 1
		k = len(S)
		a = s[0]
		while i < k:
			if S[i] != S[i - 1]:
				a += S[i]
			i += 1
		return a


class Solution:

	def removeConsecutiveCharacter(self, S):
		t = ''
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				t += S[i]
		t += S[-1]
		return t


class Solution:

	def removeConsecutiveCharacter(self, S):
		temp = list(s)
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				temp[i] = ' '
		temp = [i for i in temp if i != ' ']
		temp = ''.join(temp)
		return temp


class Solution:

	def removeConsecutiveCharacter(self, S):
		st = []
		for i in S:
			if st and st[-1] == i:
				pass
			else:
				st.append(i)
		return ''.join(st)


class Solution:

	def removeConsecutiveCharacter(self, s):
		H = ''
		for i in range(len(s)):
			if i != len(s) - 1 and s[i] == s[i + 1]:
				pass
			else:
				H += s[i]
		return H


class Solution:

	def removeConsecutiveCharacter(self, s):
		ans = s[0]
		i = 0
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				if s[i] != ans[-1]:
					ans += s[i]
			else:
				ans += s[i]
		return ans


class Solution:

	def removeConsecutiveCharacter(self, S):
		new_S = ''
		n = len(S)
		for i in range(n):
			if i > 0 and S[i] == s[i - 1]:
				continue
			else:
				new_S = new_S + S[i]
		return new_S


class Solution:

	def removeConsecutiveCharacter(self, S):
		a = str(s[0])
		for i in range(len(S) - 1):
			if S[i] == S[i + 1]:
				continue
			else:
				a += str(s[i + 1])
		return a


class Solution:

	def removeConsecutiveCharacter(self, S):
		a = ' '.join(s)
		b = a.split(' ')
		i = 1
		while i < len(b):
			if b[i] == b[i - 1]:
				b.pop(i - 1)
			else:
				i += 1
		e = ''.join(b)
		return e


class Solution:

	def removeConsecutiveCharacter(self, S):
		ans = ''
		i = 0
		j = 0
		while j < len(S):
			if S[i] == S[j]:
				j += 1
			elif S[i] != S[j] or j == len(S) - 1:
				ans += S[i]
				i = j
				j += 1
		ans += S[j - 1]
		return ans


class Solution:

	def removeConsecutiveCharacter(self, S):
		temp = S[0]
		for i in range(1, len(S)):
			if S[i] == S[i - 1]:
				pass
			else:
				temp = temp + S[i]
		return temp


class Solution:

	def removeConsecutiveCharacter(self, S):
		p = s[0]
		try:
			for x in range(1, len(s)):
				if p[-1] == s[x]:
					pass
				elif p[-1] != s[x]:
					p += s[x]
		except:
			pass
		return p


class Solution:

	def removeConsecutiveCharacter(self, S):
		l = []
		for i in range(len(S)):
			if l == []:
				l.append(S[i])
			elif l != [] and S[i] == l[-1]:
				continue
			else:
				l.append(S[i])
		str = ''
		for i in l:
			str += i
		return str


class Solution:

	def removeConsecutiveCharacter(self, S):
		lst = []
		lst.append(S[0])
		for i in range(1, len(S)):
			if lst[-1] != S[i]:
				lst.append(S[i])
		lst = list(map(str, lst))
		return ''.join(lst)


class Solution:

	def removeConsecutiveCharacter(self, S):
		res = S[0]
		i = 1
		while i < len(S):
			if S[i] != res[-1]:
				res += S[i]
			i += 1
		return res


class Solution:

	def removeConsecutiveCharacter(self, S):
		ch = '_'
		res = ''
		for x in S:
			if ch != x:
				res += x
				ch = x
		return res


class Solution:

	def removeConsecutiveCharacter(self, S):
		st = []
		k = []
		for i in S:
			k.append(i)
		st.append(k[0])
		i = 0
		while i < len(k):
			if st[-1] == k[i]:
				i = i + 1
			else:
				st.append(S[i])
				i = i + 1
		l = ''
		for i in st:
			l = l + i
		return l


class Solution:

	def removeConsecutiveCharacter(self, S):
		output = ''
		for c in S:
			if c not in output:
				output += c
			elif c not in output[-1]:
				output += c
		return output


class Solution:

	def removeConsecutiveCharacter(self, S):
		stack = []
		li = list(S)
		for i in range(len(li)):
			if len(stack) == 0:
				stack.append(li[i])
			elif li[i] == stack[-1]:
				pass
			else:
				stack.append(li[i])
		return ''.join(stack)


class Solution:

	def removeConsecutiveCharacter(self, S):
		st = [S[0]]
		for i in range(1, len(S)):
			if S[i] == S[i - 1]:
				continue
			st.append(S[i])
		return ''.join(st)


class Solution:

	def removeConsecutiveCharacter(self, s):
		x = s[0]
		for i in range(1, len(s)):
			if s[i] != s[i - 1]:
				x += s[i]
		return x


class Solution:

	def removeConsecutiveCharacter(self, S):
		stack = []
		for i in range(len(S)):
			if not stack:
				stack.append(S[i])
				continue
			if S[i] != stack[-1]:
				stack.append(S[i])
		return ''.join(stack)


class Solution:

	def removeConsecutiveCharacter(self, S):
		i = 0
		new = ''
		if len(s) < 2:
			return S
		while i < len(S) - 1:
			char = S[i]
			new += char
			while S[i] == char and i < len(S) - 1:
				i += 1
		if new[-1] != S[-1]:
			new += S[-1]
		return new


class Solution:

	def removeConsecutiveCharacter(self, S):
		st = ''
		for i in s:
			if st == '' or i != st[len(st) - 1]:
				st = st + i
		return st


class Solution:

	def removeConsecutiveCharacter(self, S):
		ans = S[0]
		i = 1
		while i < len(S):
			if S[i] == S[i - 1]:
				i += 1
				continue
			else:
				ans += S[i]
			i += 1
		return ans


class Solution:

	def solve(self, S, ind, output, n):
		if ind == n:
			return output
		if output[-1] != S[ind]:
			output += S[ind]
		return self.solve(S, ind + 1, output, n)

	def removeConsecutiveCharacter(self, S):
		temp = S[0]
		i = 1
		n = len(S)
		while i < n:
			if S[i] != S[i - 1]:
				temp += S[i]
			i += 1
		return temp


class Solution:

	def removeConsecutiveCharacter(self, S):
		temp = S[0]
		i = 1
		n = len(S)
		while i < n:
			if S[i] != S[i - 1]:
				temp += S[i]
			i += 1
		return temp


class Solution:

	def removeConsecutiveCharacter(self, S):
		l1 = []
		num = len(S)
		for i in range(num):
			if S[i] != S[i - 1] or i == 0:
				l1.append(S[i])
		return ''.join(l1)


class Solution:

	def removeConsecutiveCharacter(self, S):
		res = ''
		if S == '':
			return ''
		else:
			res += S[0]
			for i in S:
				if i == res[-1]:
					continue
				else:
					res += i
		return res


class Solution:

	def removeConsecutiveCharacter(self, S):
		new_str = s[0]
		for i in range(1, len(s)):
			if s[i] != s[i - 1]:
				new_str += s[i]
		return new_str


class Solution:

	def removeConsecutiveCharacter(self, S):
		i = 1
		new_s = S[0]
		while i < len(S):
			if new_s[-1] == S[i]:
				i += 1
			else:
				new_s += S[i]
				i += 1
		return new_s


class Solution:

	def removeConsecutiveCharacter(self, S):
		a = []
		for k in S:
			if k not in a:
				a.append(k)
			elif not k == a[len(a) - 1]:
				a.append(k)
		return ''.join(a)


class Solution:

	def removeConsecutiveCharacter(self, S):
		op = ''
		for char in S:
			if op == '' or char != op[len(op) - 1]:
				op = op + char
		return op


class Solution:

	def removeConsecutiveCharacter(self, S):
		cpy = S[0]
		for i in range(1, len(S)):
			if S[i - 1] == S[i]:
				continue
			else:
				cpy = cpy + S[i]
		return cpy


class Solution:

	def removeConsecutiveCharacter(self, S):
		b = []
		b.append(s[0])
		for i in s:
			if i == b[-1]:
				continue
			else:
				b.append(i)
		b = ''.join(b)
		return b


class Solution:

	def removeConsecutiveCharacter(self, S):
		ans = []
		for c in S:
			if len(ans) == 0:
				ans.append(c)
			elif ans[-1] != c:
				ans.append(c)
		return ''.join(ans)


class Solution:

	def removeConsecutiveCharacter(self, s):
		n = len(s)
		i = 1
		while i < n:
			if s[i] == s[i - 1]:
				s = s[:i] + s[i + 1:]
				n -= 1
				i -= 1
			i += 1
		return s


class Solution:

	def removeConsecutiveCharacter(self, s):
		a = ''
		for i in range(len(s) - 1):
			if s[i] != s[i + 1]:
				a += s[i]
		a += s[-1]
		return a


class Solution:

	def removeConsecutiveCharacter(self, s):
		r = s[0]
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				continue
			else:
				r += s[i]
		return r


class Solution:

	def removeConsecutiveCharacter(self, S):
		s1 = '' + s[0]
		for i in s:
			if s1[-1] != i:
				s1 += i
		return s1


class Solution:

	def removeConsecutiveCharacter(self, S):
		bef = ''
		res = ''
		for i in S:
			if i != bef:
				bef = i
				res += i
		return res


class Solution:

	def removeConsecutiveCharacter(self, S):
		st = s[0]
		for i in s:
			if st[-1] == i:
				continue
			else:
				st += i
		return st


class Solution:

	def removeConsecutiveCharacter(self, S):
		r = ''
		p = ''
		for char in s:
			if char not in p:
				r += char
				p = char
		return r


class Solution:

	def removeConsecutiveCharacter(self, S):
		k = '' + S[0]
		p = S[0]
		for i in range(len(S)):
			if S[i] == p:
				continue
			else:
				k = k + s[i]
			p = S[i]
		return k


class Solution:

	def removeConsecutiveCharacter(self, S):
		if len(S) == 0:
			return S
		res = [S[0]]
		for x in S[1:]:
			if res[-1] != x:
				res.append(x)
		return ''.join(res)


class Solution:

	def removeConsecutiveCharacter(self, S):
		s = '' + S[0]
		temp = S[0]
		for x in S[1:]:
			if temp != x:
				temp = x
				s += x
		return s


class Solution:

	def removeConsecutiveCharacter(self, s):
		st = ''
		s = s + '9'
		for i in range(1, len(s)):
			if s[i] != s[i - 1]:
				st = st + s[i - 1]
		return st


class Solution:

	def removeConsecutiveCharacter(self, S):
		stack = [S[0]]
		for i in range(len(S)):
			if S[i] == stack[-1]:
				continue
			else:
				stack.append(S[i])
		string = ''.join(stack)
		return string


class Solution:

	def removeConsecutiveCharacter(self, S):
		a = S[0]
		b = S[0]
		for i in S[1:]:
			if i != a:
				b += i
				a = i
		return b


class Solution:

	def removeConsecutiveCharacter(self, S):
		w = list(S)
		for i in range(len(w) - 1):
			if w[i] == w[i + 1]:
				w[i] = ''
		return ''.join(w)