mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 03:27:07 +00:00
Time: 3 ms (98.34%), Space: 17.9 MB (61.48%) - LeetHub
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
class Solution:
|
||||
def countAndSay(self, n: int) -> str:
|
||||
"""
|
||||
Complexities:
|
||||
Time: O(n2^n)
|
||||
Space: O()
|
||||
"""
|
||||
|
||||
# O(len(s)) where len(s) ~ O(2^n)
|
||||
def RLE(s: str) -> str:
|
||||
rle = ""
|
||||
checkChar = s[0]
|
||||
charCount = 0
|
||||
for i, l in enumerate(s):
|
||||
if checkChar == l:
|
||||
charCount += 1
|
||||
else:
|
||||
rle += f"{charCount}{checkChar}"
|
||||
checkChar = l
|
||||
charCount = 1
|
||||
|
||||
if checkChar:
|
||||
rle += f"{charCount}{checkChar}"
|
||||
|
||||
return rle
|
||||
|
||||
lastSol = "1"
|
||||
# Iteratively call RLE on lastSol (equiv. to countAndSay(n-1))
|
||||
# - O(n2^n) = n * O(2^n)
|
||||
for i in range(2, n + 1):
|
||||
lastSol = RLE(lastSol)
|
||||
|
||||
return lastSol
|
||||
Reference in New Issue
Block a user