Count and Say
LeetCode 38 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Term n = run-length encoding of term n−1.
நான் Count and Say (LeetCode 38) ஐ இப்படித் தீர்த்தேன் — Parsing / Conversion பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Each term describes the previous one: read it as runs of equal digits and write 'count then digit' for each run. Recursively get term n−1 and run-length encode it.
Approach
- 1
Base: n = 1 → '1'.
- 2
Get say = term(n−1).
- 3
Scan runs: count equal neighbours, append count + digit.
நான் எப்படித் தீர்த்தேன்
Recursive run-length encodingPythonO(L) per term
class Solution:
def countAndSay(self, n):
if n == 1:
return "1"
say = self.countAndSay(n - 1)
result = ""
i = 0
while i < len(say):
char = say[i]
count = 1
while i < len(say) - 1 and say[i] == say[i + 1]:
count += 1
i += 1
result += str(count) + char
i += 1
return resultCount and Say — அடிக்கடி கேட்கப்படும் கேள்விகள்
Count and Say பின்னாலுள்ள intuition என்ன?
Each term describes the previous one: read it as runs of equal digits and write 'count then digit' for each run. Recursively get term n−1 and run-length encode it.
Count and Say (LeetCode 38) ஐ எப்படித் தீர்ப்பது?
1. Base: n = 1 → '1'. 2. Get say = term(n−1). 3. Scan runs: count equal neighbours, append count + digit.
Count and Say இன் time மற்றும் space complexity என்ன?
Time: O(total length). Space: O(total length). (Accepted approach: Recursive run-length encoding.)
Count and Say இல் பொதுவான தவறுகள் என்ன?
Build the result with a list in Python; repeated string concatenation is slower.