Given a piece of code, the task is to remove all the comments from the code.
Note: 
Comments formats can be " /*...*/ " and " //... \n".
Comments cannot be nested.
Example 1:
Input:
code =
#include int main(int *argc,char **argv)
{ // First line of code\n 
printf("Hello World!!! "); return 0; }
Output:
#include int main(int *argc,char **argv)
{  printf("Hello World!!! "); return 0; }
Explanation: Removed the commented area
starting with // and ending with \n.
Example 2:
Input: 
code =
#include int main(int *argc,char **argv)
{ /* First line of code Printing
Hello World */ 
printf("Hello World!!! "); return 0; }
Output:
#include int main(int *argc,char **argv)
{  printf("Hello World!!! "); return 0; }
Explanation: Removed the commented area
starting with /* and ending with */.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeComments() which takes the string of code as input and returns the updated string of code after removing all the comments from it.
Expected Time Complexity: O(|code|)
Expected Auxiliary Space: O(1).
Constraints:
4 <= |code| <= 100000
|code| denotes the length of the string code.

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

	def removeComments (self, code):

		# your code here

Solutions:
import re

class Solution:

	def removeComments(self, code):
		pat = '//.*\\\\n'
		code = re.sub(pat, '', code)
		pat = '/\\*.*\\*/'
		code = re.sub(pat, '', code, re.MULTILINE)
		return code


class Solution:

	def removeComments(self, code):
		a = ''
		i = 0
		code = code.replace('\\n', '*/')
		code = code.replace('//', '/*')
		while i < len(code) - 1:
			if code[i] + code[i + 1] == '/*':
				j = i + 2
				while True:
					if code[j] + code[j + 1] == '*/':
						break
					j += 1
				i = j + 2
			else:
				a += code[i]
				i += 1
		if code[-2] + code[-1] != '\\n' and code[-2] + code[-1] != '*/':
			a += code[-1]
		return a


class Solution:

	def removeComments(self, code):
		i = 0
		n = len(code)
		read = 1
		ans = ''
		while i < n:
			if code[i:i + 2] == '//' or code[i:i + 2] == '/*':
				read = 0
				i += 2
			elif (code[i:i + 2] == '\\' or code[i:i + 2] == '*/' or code[i:i + 2] == '\\n') and read == 0:
				read = 1
				if code[i] == '\n':
					i += 1
					continue
				i += 2
			else:
				if read:
					ans += code[i]
				i += 1
		return ans


class Solution:

	def removeComments(self, st):
		ans = []
		read = True
		i = 0
		while i < len(st):
			if st[i:i + 2] == '//' or st[i:i + 2] == '/*':
				read = False
				i += 2
			elif (st[i:i + 2] == '\\' or st[i:i + 2] == '*/' or st[i:i + 2] == '\\n') and read == False:
				read = True
				if st[i] == '\n':
					i += 1
					continue
				i += 2
			else:
				if read:
					ans.append(st[i])
				i += 1
		return ''.join(ans)


class Solution:

	def removeComments(self, code):
		f = 0
		ans = ''
		i = 0
		while i < len(code):
			if f == 0:
				if code[i] == '/':
					if i + 1 < len(code) and code[i + 1] == '/':
						f = 1
						target = '\\n'
						i += 1
					elif i + 1 < len(code) and code[i + 1] == '*':
						f = 1
						target = '*/'
						i += 1
					else:
						ans += code[i]
				else:
					ans += code[i]
			elif code[i] == target[0]:
				if i + 1 < len(code) and code[i + 1] == target[1]:
					f = 0
					i += 1
			i += 1
		return ans


class Solution:

	def removeComments(self, code):
		s1 = '/*'
		s2 = '*/'
		s3 = '//'
		s4 = '\\n'
		s1_start = -1
		s2_end = -1
		s3_start = -1
		s4_end = -1
		for i in range(len(code) - 1):
			if code[i] == s1[0] and code[i + 1] == s1[1]:
				s1_start = i
			elif code[i] == s2[0] and code[i + 1] == s2[1]:
				s2_end = i + 1
			elif code[i] == s3[0] and code[i + 1] == s3[1]:
				s3_start = i
			elif code[i] == s4[0] and code[i + 1] == s4[1]:
				s4_end = i + 1
		if s1_start != -1 and s2_end != -1:
			code = code.replace(code[s1_start:s2_end + 1], '')
		if s3_start != -1 and s4_end != -1:
			code = code.replace(code[s3_start:s4_end + 1], '')
		return code


class Solution:

	def removeComments(self, code):
		slash_star = False
		slash_slash = False
		out_code = ''
		i = 0
		while i < len(code) - 1:
			if slash_star and code[i] == '*' and (code[i + 1] == '/'):
				slash_star = False
				i += 1
			elif slash_slash and code[i] == '\\' and (code[i + 1] == 'n'):
				slash_slash = False
				i += 1
			elif code[i] == '/' and code[i + 1] == '*':
				slash_star = True
				i += 1
			elif code[i] == '/' and code[i + 1] == '/':
				slash_slash = True
				i += 1
			elif not slash_slash and (not slash_star):
				out_code += code[i]
			i += 1
		if i < len(code):
			out_code += code[-1]
		return out_code


import re

class Solution:

	def removeComments(self, code):
		pat = '\\/\\/.*\\\\n'
		res = re.sub(pat, '', code)
		pat2 = '\\/\\*.*\\*\\/'
		res2 = re.sub(pat2, '', res)
		return res2
if __name__ == '__main__':
	t = int(input())
	for tc in range(t):
		s = input()
		ob = Solution()
		print(ob.removeComments(s))


from collections import OrderedDict

class Solution:

	def removeComments(self, code):
		code_removal = OrderedDict()
		string = ''
		(single_line, multi_line) = (False, False)
		start_mul = -1
		for (idx, i) in enumerate(code):
			string += i
			if string == '/*':
				if not (single_line or multi_line):
					start_mul = idx - 1
					multi_line = True
			elif string == '*/':
				if multi_line:
					code_removal[start_mul] = idx
					start_mul = -1
					multi_line = False
			elif string == '//':
				if not (single_line or multi_line):
					single_line = True
					start_single = idx - 1
			elif string == '\\n':
				if single_line:
					code_removal[start_single] = idx
					single_line = False
					start_single = -1
			elif string == '/' or string == '*':
				continue
			elif string == '\\':
				continue
			string = ''
		string = ''
		prev_val = 0
		for (key, value) in code_removal.items():
			string += code[prev_val:key]
			prev_val = value + 1
		string += code[prev_val:]
		return string


class Solution:

	def removeComments(self, code):
		m1 = {}
		strt = 0
		end = 0
		comment_flag = None
		for i in range(len(code) - 1):
			if code[i] and code[i + 1]:
				if code[i:i + 2] == '//':
					comment_flag = 1
					strt = i
				if code[i:i + 2] == '/*':
					comment_flag = 2
					strt = i
				if code[i:i + 2] == '\\n' and comment_flag == 1:
					end = i
					m1[strt, end] = 1
				if code[i:i + 2] == '*/' and comment_flag == 2:
					end = i
					m1[strt, end] = 1
		l = 0
		for i in m1.keys():
			code = code[:i[0] - l] + code[i[1] - l + 2:]
			l += i[1] - i[0]
		return code


class Solution:

	def removeComments(self, code):
		l = ['']
		i = 0
		while i < len(code):
			if code[i] == '/' and i != len(code) - 1:
				if code[i + 1] == '*' or code[i + 1] == '/':
					l.append(code[i])
					l[-1] += code[i + 1]
					i += 1
				else:
					l[-1] += code[i]
			elif i != len(code) - 1 and code[i] == '*' or code[i] == '\\':
				if code[i + 1] == '/' or code[i + 1] == 'n':
					l.pop()
					i += 1
				else:
					l[-1] += code[i]
			else:
				l[-1] += code[i]
			i += 1
		if l[-1][0] == '/':
			if l[-1][1] == '*' or l[-1][1] == '/':
				l.pop()
		return ''.join(l)


import re

class Solution:

	def removeComments(self, code):
		ans = []
		read = True
		i = 0
		while i < len(code):
			if code[i:i + 2] == '//' or code[i:i + 2] == '/*':
				read = False
				i += 2
			elif (code[i:i + 2] == '\\' or code[i:i + 2] == '*/' or code[i:i + 2] == '\\n') and read == False:
				read = True
				if code[i] == '\n':
					i += 1
					continue
				i += 2
			else:
				if read:
					ans.append(code[i])
				i += 1
		c = ''.join(ans)
		return c


class Solution:

	def removeComments(self, code):
		s_cmt = False
		m_cmt = False
		ans = ''
		i = 0
		while i < len(code):
			if code[i] == '/':
				if code[i + 1] == '/':
					s_cmt = True
					i += 2
				elif code[i + 1] == '*':
					m_cmt = True
					i += 2
			if ord(code[i]) == 92 and code[i + 1] == 'n' and (s_cmt == True):
				s_cmt = False
				i += 2
			if code[i] == '*' and code[i + 1] == '/' and (m_cmt == True):
				m_cmt = False
				i += 2
			if s_cmt == True or m_cmt == True:
				i += 1
			else:
				ans += code[i]
				i += 1
		return ans


class Solution:

	def removeComments(self, code):
		ans = ''
		i = 0
		n = len(code)
		while i < n:
			if i + 1 < n:
				if ord(code[i]) == 47 and ord(code[i + 1]) == 47:
					i += 2
					while code[i:i + 2] != '\\n':
						i += 1
					i += 2
				elif ord(code[i]) == 47 and code[i + 1] == '*':
					i += 2
					while i + 1 < n and code[i] != '*' and (ord(code[i + 1]) != 47):
						i += 1
					i += 2
				else:
					ans += code[i]
					i += 1
			else:
				ans += code[i]
				i += 1
		return ans


class Solution:

	def removeComments(self, don):
		last = '\n'
		code = ''
		for i in don:
			code += i
		ind = 0
		res = ''
		while ind < len(code):
			if code[ind] == '/' and code[ind + 1] == '/':
				i = ind + 1
				while i < len(code) - 1:
					if ord(code[i]) == 92 and code[i + 1] == 'n':
						ind = i
						break
					i += 1
				ind = i + 2
			elif code[ind] == '/' and code[ind + 1] == '*':
				i = ind + 1
				while i < len(code) - 1:
					if code[i] == '*' and code[i + 1] == '/':
						ind = i
						break
					i += 1
				ind = i + 2
			else:
				res += code[ind]
				ind += 1
		return res


import re

class Solution:

	def removeComments(self, code):
		pat = '\\/\\/.*\\\\n'
		res = re.sub(pat, '', code)
		pat2 = '\\/\\*.*\\*\\/'
		res2 = re.sub(pat2, '', res)
		return res2


class Solution:

	def removeComments(self, code):
		t = code
		strs = ''
		count = 0
		for x in range(0, len(code) - 2):
			if code[x] + code[x + 1] == '//' or code[x] + code[x + 1] == '/*':
				strs += code[x] + code[x + 1]
				x = x + 1
			if len(strs) > 0:
				strs += code[x]
			if '\\n' in code[x] + code[x + 1] and strs[0] + strs[1] == '//':
				strs = strs + code[x + 1]
				t = t.replace(strs[2:], '')
				x = x + 1
				strs = ''
			if code[x] + code[x + 1] == '*/' and strs[0] + strs[1] == '/*':
				strs = strs + code[x + 1]
				strs = strs.replace('**', '')
				t = t.replace(strs, '')
				x = x + 1
				strs = ''
		return t


class Solution:

	def removeComments(self, code):
		import re
		code = re.sub('/[*].*[*]/', '', code)
		code = re.sub('//.*\\\\n', '', code)
		return code


class Solution:

	def removeComments(self, code):
		count = 0
		s = ''
		for i in range(len(code) + 1):
			try:
				if code[i] + code[i + 1] == '/*' or code[i] + code[i + 1] == '//':
					count = 1
				if code[i - 2] + code[i - 1] == '*/' or code[i - 2] + code[i - 1] == '\\n':
					count = 0
				if count == 0:
					s += code[i]
			except IndexError:
				pass
		return s + code[-1]