The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221



1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.



Given an integer n, generate the nth term of the count-and-say sequence.



Note: Each term of the sequence of integers will be represented as a string.


Example 1:

Input: 1
Output: "1"



Example 2:

Input: 4
Output: "1211"

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Starter code: class Solution:
    def countAndSay(self, n: int) -> str:
        

Solutions:
class Solution:

	def countAndSay(self, n):
		s = '1'
		for _ in range(n - 1):
			(let, temp, count) = (s[0], '', 0)
			for l in s:
				if let == l:
					count += 1
				else:
					temp += str(count) + let
					let = l
					count = 1
			temp += str(count) + let
			s = temp
		return s


class Solution:

	def countAndSay(self, n):
		s = '1'
		for _ in range(n - 1):
			(temp, pre, count) = ('', s[0], 0)
			for i in s:
				if i == pre:
					count += 1
				else:
					temp += str(count) + pre
					count = 1
					pre = i
			temp += str(count) + pre
			s = temp
		return s


class Solution:

	def countAndSay(self, n):
		L = '1'
		if n == 1:
			return L
		while n > 1:
			M = ''
			i = 0
			while i < len(L):
				k = 1
				x = L[i]
				while i < len(L) - 1 and L[i + 1] == x:
					i = i + 1
					k = k + 1
				M = M + str(k) + x
				i = i + 1
			n = n - 1
			L = M
		return L


class Solution:

	def countAndSay(self, n):
		s = '1'
		for _ in range(n - 1):
			s = re.sub('(.)\\1*', lambda m: str(len(m.group(0))) + m.group(1), s)
		return s


class Solution:

	def countAndSay(self, n):
		if n < 1:
			raise ValueError('Input should be greater than or equal to 1')
		elif n == 1:
			return '1'
		currentCharacter = ''
		currentCharacterCount = 0
		result = list()
		for character in list(self.countAndSay(n - 1)):
			if character != currentCharacter:
				if currentCharacter != '':
					result.extend([str(currentCharacterCount), currentCharacter])
				currentCharacter = character
				currentCharacterCount = 1
			else:
				currentCharacterCount += 1
		result.extend([str(currentCharacterCount), currentCharacter])
		return ''.join(result)


class Solution:

	def countAndSay(self, n):
		s = ['1']
		result = '1'
		for i in range(n - 1):
			start = 0
			temp = []
			while start < len(s):
				count = 1
				next = start + 1
				while next < len(s) and s[start] == s[next]:
					next += 1
					count += 1
				temp.append(str(count))
				temp.append(s[start])
				start = next
			result = ''.join(temp)
			s = temp
		return result


class Solution:

	def countAndSay(self, n):
		if n == 1:
			return '1'
		seq = self.countAndSay(n - 1)
		seq2 = ''
		count = 0
		for i in range(len(seq)):
			num = seq[i]
			count += 1
			if i == len(seq) - 1:
				seq2 = seq2 + str(count) + num
			elif seq[i + 1] != seq[i]:
				seq2 = seq2 + str(count) + num
				count = 0
		return seq2


class Solution:

	def countAndSay(self, n):
		s = [['1']]
		for i in range(n):
			str1 = s[i]
			temp = []
			j = 0
			while j < len(str1):
				count = 1
				while j <= len(str1) - 2 and str1[j] == str1[j + 1]:
					count += 1
					j += 1
				temp.append(str(count))
				temp.append(str1[j])
				j += 1
			s.append(temp)
		res = [''.join(s[n - 1])]
		return res[0]


class Solution:

	def countAndSay(self, n):
		res = '1'
		if n < 2:
			return res
		while 1:
			n -= 1
			if n == 0:
				return res
			res = self._say(res)

	def _say(self, s):
		s += '*'
		(curr, res) = ('', '')
		t = 0
		for (i, c) in enumerate(s):
			if i == 0:
				curr = c
				t = 1
			elif c == curr:
				t += 1
			else:
				res = res + str(t) + curr
				t = 1
				curr = c
		return res


class Solution:

	def countAndSay(self, n):
		if n == 0:
			return 0
		else:
			s = '1'
			for i in range(n - 1):
				res = ''
				k = 0
				m = len(s)
				count = 1
				while k < m:
					say = s[k]
					if k + 1 < m:
						if s[k] == s[k + 1]:
							count += 1
							k += 1
						else:
							res += str(count)
							res += say
							count = 1
							k += 1
					else:
						res += str(count)
						res += say
						break
				s = res
			return s


class Solution:

	def countAndSay(self, n):
		if n == 1:
			return '1'
		n = n - 1
		string = '1'
		for i in range(n):
			a = string[0]
			count = 0
			final = ''
			for s in string:
				if a != s:
					final += str(count)
					final += a
					count = 1
					a = s
				else:
					count += 1
			final += str(count)
			final += s
			string = final
		return final