Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 38 · Parsing / Conversion

Count and Say

LeetCode 38 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Term n = run-length encoding of term n−1.

मध्यमString

मी 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. 1

    Base: n = 1 → '1'.

  2. 2

    Get say = term(n−1).

  3. 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 result
स्टेप-थ्रू लोड होत आहे…

Count 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.