Skip to content

Commit 44783b6

Browse files
committed
solves count and say in python
1 parent 3641026 commit 44783b6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RemoveElement.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/remove_element.py)|
2020
| 28 | [Needle in Haystack](https://leetcode.com/problems/implement-strstr) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/NeedleInHaystack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/needle_in_haystack.py) |
2121
| 35 | [Search Inserted Position](https://leetcode.com/problems/search-insert-position/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/SearchInsertPosition.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/search_insert_position.py) |
22-
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say) | Medium | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/CountAndSay.java) |
22+
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say) | Medium | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/CountAndSay.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/count_and_say.py) |
2323
| 53 | [Maximum SubArray](https://leetcode.com/problems/maximum-subarray) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MaximumSubArray.java) |
2424
| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LengthOfLastWord.java) |
2525
| 66 | [Plus One](https://leetcode.com/problems/plus-one) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PlusOne.java) |

Diff for: python/count_and_say.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def countAndSay(self, n: int) -> str:
3+
if n == 1:
4+
return '1'
5+
6+
previous = self.countAndSay(n - 1)
7+
# print('previous', n - 1, previous)
8+
current = previous[0]
9+
frequency = 0
10+
result = ''
11+
for number in previous:
12+
if number == current:
13+
frequency += 1
14+
else:
15+
result += f'{frequency}{current}'
16+
frequency = 1
17+
current = number
18+
result += f'{frequency}{current}' if frequency > 0 else ''
19+
return result

0 commit comments

Comments
 (0)