Skip to content

Commit e4b9a2c

Browse files
PlooshMohamad655
authored andcommitted
Add strings folder and add solution 38. Count and Say - LeetCode
1 parent 3b46508 commit e4b9a2c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Solves LeetCode question 38. Count and Say
2+
3+
class Solution(object):
4+
def countAndSay(self, n):
5+
"""
6+
:type n: int
7+
:rtype: str
8+
"""
9+
s = "1"
10+
if n == 1:
11+
return s
12+
for i in range(2,n+1):
13+
j = 0
14+
temp = ""
15+
curr = ""
16+
count = 0
17+
while j<len(s):
18+
if curr =="":
19+
curr=s[j]
20+
count=1
21+
j+=1
22+
elif curr == s[j]:
23+
count+=1
24+
j+=1
25+
else:
26+
temp+= str(count) + curr
27+
curr=""
28+
count = 0
29+
temp+=str(count) + curr
30+
s=temp
31+
return s
32+
ob1 = Solution()
33+
print(ob1.countAndSay(6))

0 commit comments

Comments
 (0)