Count and Say [leetcode easy]

Leetcode38

Example,

Input: 4
Output: “1211”
Explanation: “1” -> “11” -> “21” -> “1211”; add the count of numeric character infront and repeat for n times

Solution 1: Store the count and number for n times

time complexiity: O(n)
space complexity: O(n)
runtime: 36 ms | faster than 38.86% submissions
memory: 15 MB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def countAndSay(self, n: int) -> str:
string = '1'

while n > 1:
result = ''
count = 1

for j in range(len(string)):
if j == len(string)-1 or string[j] != string[j+1]:
result += str(count) + str(string[j])
count = 1
else:
count += 1

string = result

n -= 1

return string

Today,

I did two easy questions that don’t look easy to me. #stillproudofmyself