Geek is celebrating holi by aiming coloured water balloons at his friends. He wants to avoid throwing balloons of the same colours more than twice in a row. Given a string st, where each alphabet denotes a different colour, modify the sequenuence to represent the correct order of throwing balloons by eliminating some of them.
Example 1:
Input: 
st = ABBBCCDEAB
Output: ABBCCDEAB
Explaination: Remove the third 'B'.
Example 2:
Input: 
st = AAAAAA
Output: AA
Explaination: We can keep only two A's.
 
Your Task:
You do not need to read input or print anything. Your task is to complete the function sequence() which takes string st as input parameter and returns a string containing the modified sequence.
 
Expected Time Complexity: O(N), where n is the length of the string st.
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ |st| ≤ 10000

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def sequence(self, st):

        # code here

Solutions:
class Solution:

	def sequence(self, st):
		chars = []
		count = 0
		prev = None
		for chr in st:
			if prev != chr:
				chars.append(chr)
				prev = chr
				count = 1
			else:
				count += 1
				if count == 2:
					chars.append(chr)
		return ''.join(chars)


class Solution:

	def sequence(self, st):
		n = len(st)
		if n < 3:
			return st
		output = st[:2]
		for i in range(2, n):
			if st[i] != output[-1] or st[i] != output[-2]:
				output += st[i]
		return output


class Solution:

	def sequence(self, st):
		l = []
		c = 0
		for i in range(len(st) - 1):
			if st[i] == st[i + 1]:
				c += 1
				if c >= 2:
					pass
				else:
					l.append(st[i])
			else:
				c = 0
				l.append(st[i])
		l.append(st[-1])
		l = ''.join(l)
		return l


class Solution:

	def sequence(self, st):
		c = 0
		s = []
		for i in range(len(st) - 1):
			s.append(st[i])
			if st[i] == st[i + 1]:
				c = c + 1
				if c > 1:
					s.pop()
			else:
				c = 0
		s.append(st[len(st) - 1])
		return ''.join(s)


class Solution:

	def sequence(self, st):
		new_string = st[0]
		new_string_i = 0
		same_chr_count = 1
		for i in range(1, len(st)):
			if new_string[new_string_i] == st[i]:
				same_chr_count += 1
				if same_chr_count <= 2:
					new_string += st[i]
					new_string_i += 1
			else:
				new_string += st[i]
				new_string_i += 1
				same_chr_count = 1
		return new_string


class Solution:

	def sequence(self, st):
		ans = ''
		for i in range(len(st) - 2):
			arr = st[i:i + 3]
			if len(set(arr)) <= 1:
				pass
			else:
				ans += st[i]
		return ''.join(ans) + st[-2:]


class Solution:

	def sequence(self, st):
		if len(st) == 1:
			return st
		k = st[0] + st[1]
		for i in range(2, len(st) - 1):
			if st[i] == st[i + 1] and k[-1] == st[i]:
				k = k
			else:
				k += st[i]
		if st[-1] == k[-1] and st[-1] == k[-2]:
			return k
		return k + st[-1]


class Solution:

	def sequence(self, st):
		out = ''
		i = 0
		while i < len(st):
			if len(out) < 2:
				out += st[i]
				i += 1
			elif out[-1] == out[-2] == st[i]:
				i += 1
			else:
				out += st[i]
				i += 1
		return out


class Solution:

	def sequence(self, st):
		answer = []
		ptr = None
		counter = 0
		for item in st:
			if item != ptr:
				answer.append(item)
				ptr = item
				counter = 1
			else:
				counter += 1
				if counter == 2:
					answer.append(item)
		return ''.join(answer)


class Solution:

	def sequence(self, st):
		c = []
		cn = 0
		p = None
		for i in st:
			if p != i:
				c.append(i)
				p = i
				cn = 1
			else:
				cn += 1
				if cn == 2:
					c.append(i)
		return ''.join(c)


class Solution:

	def sequence(self, st):
		(s, k) = ('', 0)
		for i in range(len(st) - 1):
			if st[i] != st[i + 1]:
				s += st[i]
				k = 0
			elif st[i] == st[i + 1] and k < 1:
				s += st[i]
				k += 1
			else:
				pass
		s += st[-1]
		return s


class Solution:

	def sequence(self, st):
		n = len(st)
		count = 1
		ans = ''
		for i in range(1, n):
			if st[i] == st[i - 1]:
				count += 1
			else:
				if count > 1:
					ans += st[i - 1] * 2
				else:
					ans += st[i - 1]
				count = 1
		if count > 1:
			ans += st[n - 1] * 2
		else:
			ans += st[n - 1]
		return ans


class Solution:

	def sequence(self, st):
		t = ''
		m = {}
		n = len(st)
		c = 1
		for i in range(n - 1):
			if st[i] == st[i + 1]:
				c += 1
			else:
				if c == 1:
					t += st[i]
				else:
					t += st[i] + st[i]
				c = 1
		if c == 1:
			t += st[n - 1]
		else:
			t += st[n - 1] + st[n - 1]
		return t


class Solution:

	def sequence(self, st):
		count = 1
		x = st[0]
		for i in range(1, len(st)):
			if st[i] == st[i - 1]:
				count += 1
				if count <= 2:
					x += st[i]
			else:
				count = 1
				x += st[i]
		return x


class Solution:

	def sequence(self, st):
		s = st[0]
		count = 1
		for i in range(1, len(st)):
			if st[i] == st[i - 1]:
				count += 1
				if count < 3:
					s = s + st[i]
				else:
					continue
			else:
				count = 1
				s = s + st[i]
		return s


class Solution:

	def sequence(self, st):
		s = ''
		for i in range(0, len(st)):
			if len(st[i:3 + i]) < 3:
				s += st[i]
			elif len(set(st[i:3 + i])) != 1:
				s += st[i]
		return s


class Solution:

	def sequence(self, st):
		ans = ''
		for i in st:
			if len(ans) < 2:
				ans += i
			elif ans[-1] == ans[-2]:
				if ans[-1] != i:
					ans += i
			else:
				ans += i
		return ans


class Solution:

	def sequence(self, st):
		l = len(st)
		if l <= 2:
			return st
		fs = st[0:2]
		for j in range(2, l):
			if fs[-1] == fs[-2] and fs[-2] == st[j]:
				continue
			fs += st[j]
		return fs


class Solution:

	def sequence(self, st):
		c = 1
		a = ''
		for i in range(len(st) - 1):
			if c <= 2:
				a = a + st[i]
			if st[i] == st[i + 1]:
				c = c + 1
			else:
				c = 1
		if c <= 2:
			a = a + st[len(st) - 1]
		return a


class Solution:

	def sequence(self, S):
		i = 0
		while i < len(S) - 2:
			if S[i] != S[i + 1]:
				i = i + 1
			elif S[i] == S[i + 1]:
				if S[i] == S[i + 2]:
					S = S[:i + 1] + S[i + 2:]
					continue
				else:
					i = i + 2
		return S