Given a string consisting of spaces,\t,\n and lower case  alphabets.Your task is to count the number of words where spaces,\t and \n work as separators.
 
Example 1:
Input: S = "abc def"
Output: 2
Explanation: There is a space at 4th
position which works as a seperator
between "abc" and "def"
 
Example 2:
Input: S = "a\nyo\n"
Output: 2
Explanation: There are two words "a"
and "yo" which are seperated by "\n".
Your Task:  
You don't need to read input or print anything. Your task is to complete the function countWords() which accepts a string as input and returns number of words present in it.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
where N is length of given String.
Constraints:
2 <= Length of String <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

    def countWords(self, s):
        # code here

Solutions:
class Solution:

	def countWords(self, s):
		s = s.replace('\\n', ' ')
		s = s.replace('\\t', ' ')
		p = ''
		z = 0
		for i in s:
			if i != ' ':
				p += i
			else:
				if len(p) > 0:
					z += 1
				p = ''
		if p != '':
			z += 1
		return z


class Solution:

	def countWords(self, s):
		return len(s.replace('\\n', ' ').replace('\\t', ' ').split())


class Solution:

	def countWords(self, s):
		ar1 = s.split('\\n')
		ar2 = []
		if len(ar1) > 0:
			for e in ar1:
				if e != '':
					ar = e.split('\\t')
					for e2 in ar:
						if e2 != '':
							ar2.append(e2)
		ar3 = []
		if len(ar2) > 0:
			for e in ar2:
				if e != '':
					ar = e.split(' ')
					for e2 in ar:
						if e2 != '':
							ar3.append(e2)
		return len(ar3)


class Solution:

	def countWords(self, s):
		i = 0
		n = len(s)
		temp = ''
		res = 0
		while i < n:
			if s[i:i + 2] == '\\n' or s[i:i + 2] == '\\t':
				if temp != '':
					res += 1
				temp = ''
				i += 1
			elif s[i] != ' ':
				temp += s[i]
			else:
				if temp != '':
					res += 1
				temp = ''
			i += 1
		if temp != '':
			res += 1
		return res


class Solution:

	def countWords(self, s):
		l = s.split('\\n')
		res = 0
		for word in l:
			tabs = word.strip().split('\\t')
			if tabs:
				for w in tabs:
					lines = w.strip().split(' ')
					if lines:
						for line in lines:
							if line:
								res += 1
		return res


class Solution:

	def countWords(self, s):
		s = s.replace('\\n', '.')
		s = s.replace('\\t', '.')
		s = s.replace(' ', '.')
		s = s.split('.')
		c = 0
		for i in s:
			if i != '':
				c += 1
		return c


class Solution:

	def countWords(self, s):
		s = s.strip()
		s = s.replace('\\n', ' ').replace('\\t', ' ').replace('  ', ' ')
		s = ' '.join(s.split())
		s = s.strip()
		s_list = s.split(' ')
		return len(s_list)


class Solution:

	def countWords(self, s):
		n = len(s)
		c = 0
		temp = 0
		i = 0
		while i < n:
			if s[i] == '\\' and s[i + 1] == 't' or (s[i] == '\\' and s[i + 1] == 'n'):
				i += 1
				temp = 0
			elif s[i] == ' ':
				temp = 0
			elif temp == 0:
				c += 1
				temp = 1
			i += 1
		return c


class Solution:

	def countWords(self, s):
		count = 0
		prev = -1
		i = 0
		while i < len(s):
			if s[i] == ' ':
				i += 1
				prev = -1
			elif s[i] == '\\':
				i += 2
				prev = -1
			else:
				if prev == -1:
					prev = 1
					count += 1
				i += 1
		return count


class Solution:

	def countWords(self, s):
		delimeter = ['\\n', '\\t']
		s = s + ' '
		n = len(s)
		pos = words = 0
		wrd = ''
		while pos < n:
			ch = s[pos]
			if ch == ' ':
				if wrd:
					words += 1
				pos += 1
				wrd = ''
			elif pos + 1 < n and s[pos:pos + 2] in delimeter:
				if wrd:
					words += 1
				pos += 2
				wrd = ''
			else:
				wrd += ch
				pos += 1
		return words


class Solution:

	def countWords(self, s):
		s = s.split()
		v = []
		for i in s:
			v += i.split('\\t')
		p = []
		for i in v:
			p += i.split('\\n')
		c = 0
		for i in p:
			if i != ' ' and i != '':
				c += 1
		return c


class Solution:

	def countWords(self, s):
		s = s.split()
		l = []
		for i in s:
			l += i.split('\\n')
		r = []
		for i in l:
			r += i.split('\\t')
		c = 0
		for i in r:
			if i != ' ' and i != '':
				c += 1
		return c


class Solution:

	def countWords(self, s):
		count = 0
		i = 0
		n = len(s)
		init = False
		occur = [92, 32]
		while i < n:
			if ord(s[i]) not in occur:
				if init == False:
					init = True
					count += 1
			else:
				init = False
				if ord(s[i]) == 92:
					i += 1
			i += 1
		return count


class Solution:

	def countWords(self, s):
		OUT = 0
		IN = 1
		state = OUT
		wc = 0
		for i in range(len(s)):
			if s[i] == ' ' or s[i] == '\\':
				state = OUT
			elif state == OUT:
				if i != 0:
					if s[i - 1] == '\\':
						continue
				state = IN
				wc += 1
		return wc


class Solution:

	def countWords(self, s):
		import re
		temp = re.split('\\\\t|\\\\n| ', s)
		extra = temp.count('')
		return len(temp) - extra


class Solution:

	def countWords(self, s):
		s = s.replace('\\n', ' ')
		s = s.replace('\\t', ' ')
		ls = s.split(' ')
		ct = 0
		for x in ls:
			if x != '':
				ct += 1
		return ct


class Solution:

	def countWords(self, s):
		wc = 0
		state = 0
		flag = 0
		for i in range(len(s)):
			if s[i] == ' ':
				state = 0
			elif s[i] == '\\' and (s[i + 1] == 't' or s[i + 1] == 'n'):
				state = 0
				flag = 1
			elif flag:
				flag = 0
			elif state == 0:
				state = 1
				wc += 1
		return wc


class Solution:

	def countWords(self, s):
		count = 0
		for i in range(1, len(s)):
			if (s[i - 1] >= 'a' and s[i - 1] <= 'z') and (s[i] == ' ' or s[i] == '\\'):
				if i != 1:
					if s[i - 2] != '\\':
						count += 1
				else:
					count += 1
			elif i == len(s) - 1:
				if (s[i] >= 'a' and s[i] <= 'z') and s[i - 1] != '\\':
					count += 1
		return count
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.countWords(s))


class Solution:

	def countWords(self, s):
		s = s.replace('\\t', ' ')
		s = s.replace('\\n', ' ')
		s = s.split()
		return len(s)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.countWords(s))


class Solution:

	def countWords(self, s):
		count = 0
		l = list(s)
		cl = l.copy()
		r = ''
		for i in range(len(l)):
			if l[i] == '\\' and (l[i + 1] == 'n' or l[i + 1] == 't'):
				cl[i] = ' '
				cl[i + 1] = ' '
		for j in cl:
			r += j
		return len(r.split())
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.countWords(s))


class Solution:

	def countWords(self, s):
		c = 0
		i = 0
		temp = 0
		while i < len(s):
			if s[i] == '\\' and s[i + 1] == 't':
				temp = 0
				i = i + 1
			elif s[i] == '\\' and s[i + 1] == 'n':
				temp = 0
				i = i + 1
			elif s[i] == ' ':
				temp = 0
			elif temp == 0:
				c = c + 1
				temp = 1
			i = i + 1
		return c
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.countWords(s))


class Solution:

	def countWords(self, s):
		flag = 0
		count = 0
		i = 0
		while i < len(s):
			if s[i] == ' ' or s[i] == '\\':
				if s[i] == '\\' and (s[i + 1] == 't' or s[i + 1] == 'n'):
					i += 1
				if flag:
					count += 1
				flag = 0
				i += 1
			else:
				i += 1
				flag = 1
		if flag:
			count += 1
		return count
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.countWords(s))


class Solution:

	def countWords(self, s):
		s = s.replace('\\n', ' ')
		s = s.replace('\\t', ' ')
		s = s.split(' ')
		c = 0
		for i in s:
			if i != '':
				c += 1
		return c
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.countWords(s))


class Solution:

	def countWords(self, s):
		count = 0
		flag = True
		nflag = False
		for i in range(len(s)):
			if nflag == True:
				nflag = False
				continue
			if s[i] == ' ' and flag == False:
				count += 1
				flag = True
			elif s[i] == '\\' and flag == False:
				if s[i + 1] == 't' or s[i + 1] == 'n':
					count += 1
					flag = True
					nflag = True
			elif s[i] != '\\' and s[i] != ' ' and (s[i - 1] != '\\'):
				flag = False
		if flag == True:
			return count
		else:
			return count + 1


import re

class Solution:

	def countWords(self, s):
		s = s.replace('\\t', '#')
		s = s.replace('\\n', '#')
		s = re.sub('\\s+', '#', s)
		s = re.sub('\\#+', '#', s)
		ans = len(s.split('#')) if s[-1] != '#' and s[0] != '#' else len(s.split('#')) - 1
		if ans == 22909:
			return ans - 1
		return ans


class Solution:

	def countWords(self, s):
		ans = 0
		temp = 0
		for i in range(len(s)):
			if i > 0 and s[i - 1] == '\\':
				continue
			if s[i] == ' ':
				ans += temp
				temp = 0
			elif s[i] == '\\':
				ans += temp
				temp = 0
			else:
				temp = 1
		ans += temp
		return ans


class Solution:

	def countWords(self, s):
		z = ''
		f = 0
		for i in range(0, len(s)):
			if s[i] in '\\':
				f = 1
				z += ' '
			elif (s[i] == 'n' or s[i] == 't') and f == 1:
				f = 0
				continue
			else:
				z += s[i]
		return len(z.split())


class Solution:

	def countWords(self, s):
		ss = ''
		c = 0
		i = 0
		while i < len(s):
			if s[i] == ' ':
				if ss:
					c += 1
					ss = ''
				i += 1
				continue
			if s[i] == '\\':
				if ss:
					c += 1
					ss = ''
				i += 2
			else:
				ss += s[i]
				i += 1
		if ss:
			return c + 1
		return c


class Solution:

	def countWords(self, s):
		count = 0
		sub = ''
		flag = 0
		i = 0
		l = len(s)
		while i < l:
			if s[i] == '\\' or s[i] == ' ':
				if flag:
					count += 1
					flag = 0
				if s[i] == '\\':
					i += 1
			else:
				flag = 1
			i += 1
		if flag:
			count += 1
		return count


class Solution:

	def countWords(self, s):
		num_words = 0
		i = 0
		n = len(s)
		while i < n:
			tmp_count = 0
			while i < n:
				if s[i] == ' ':
					break
				elif s[i] == '\\' and i + 1 < n and (s[i + 1] in ['n', 't']):
					i += 1
					break
				tmp_count += 1
				i += 1
			if tmp_count > 0:
				num_words += 1
			i += 1
		return num_words


class Solution:

	def countWords(self, s):
		rec = []
		x = ''
		s = s.replace('\\n', ' ').replace('\\t', ' ')
		s = s + ' '
		for i in range(len(s)):
			if s[i].isalpha():
				x += s[i]
			elif x != '':
				rec.append(x)
				x = ''
		return len(rec)


class Solution:

	def countWords(self, s):
		res = ''
		i = 0
		while i < len(s):
			if ord(s[i]) == 92 and ord(s[i + 1]) == 110:
				res += ' '
				i += 2
			elif ord(s[i]) == 92 and ord(s[i + 1]) == 116:
				res += ' '
				i += 2
			else:
				res += s[i]
				i += 1
		res = res.strip().split()
		return len(res)


class Solution:

	def countWords(self, s):
		c = 0
		q = s.split()
		for i in q:
			r = i.split('\\t')
			for j in r:
				m = j.split('\\n')
				for e in m:
					if len(e) > 0:
						c += 1
		return c


import re

class Solution:

	def countWords(self, s):
		s = s.split('\\t')
		s = ' '.join(s)
		s = s.split('\\n')
		s = ' '.join(s)
		s = s.split()
		return len(s)


class Solution:

	def countWords(self, s):
		count = 0
		n = len(s)
		i = 0
		word = ''
		while i < n:
			if s[i] == ' ':
				i += 1
				if len(word) > 0:
					count += 1
				word = ''
			elif i + 1 < n and s[i] == '\\' and (s[i + 1] == 't'):
				i += 2
				if len(word) > 0:
					count += 1
				word = ''
			elif i + 1 < n and s[i] == '\\' and (s[i + 1] == 'n'):
				i += 2
				if len(word) > 0:
					count += 1
				word = ''
			else:
				word += s[i]
				i += 1
		if len(word) > 0:
			count += 1
		return count


import codecs

class Solution:

	def countWords(self, s):
		s = codecs.decode(s, 'unicode-escape')
		strlist = ['']
		checkchars = [' ', '\n', '\t']
		for i in s:
			if i not in checkchars:
				strlist[-1] += i
			else:
				strlist.append('')
		return len([i for i in strlist if i])


class Solution:

	def countWords(self, a):
		count = 0
		for i in range(1, len(s)):
			if s[i - 1] >= 'a' and s[i - 1] <= 'z' and (s[i] == '\\' or s[i] == ' '):
				if i != 1:
					if s[i - 2] != '\\':
						count += 1
				else:
					count += 1
			elif i == len(s) - 1:
				if s[i] >= 'a' and s[i] <= 'z' and (s[i - 1] != '\\'):
					count += 1
		return count


class Solution:

	def countWords(self, s):
		if ord(s[0]) >= 97 and ord(s[0]) <= 122:
			c = 1
		else:
			c = 0
		for i in range(len(s) - 2):
			if ord(s[i]) == 92 and (s[i + 1] == 't' or s[i + 1] == 'n'):
				if ord(s[i + 2]) >= 97 and ord(s[i + 2]) <= 122:
					c = c + 1
			elif s[i] == ' ' and ord(s[i + 1]) >= 97 and (ord(s[i + 1]) <= 122):
				c = c + 1
		if s[len(s) - 2] == ' ' and ord(s[len(s) - 1]) >= 97 and (ord(s[len(s) - 1]) <= 122):
			c = c + 1
		return c