Modular C# Solution Easy To Understand


  • 0
    C
     public string CountAndSay(int n) {
        
        var seed = "1";
    
        for (int i=0; i < n - 1; i++)
        {
            seed = GetSay(seed);    
        }
        
        return seed;
    }
    
    public string GetSay(string seed)
    {
        if (seed.Length == 1)
        {
            return "1" + seed;
        }
        
        var result = "";
        var curCount = 1;
        var curChar = seed[0];    
        
        for (int i=1; i < seed.Length; i++)
        {
            if (curChar == seed[i])
            {
                curCount++;
            }
            else
            {
                result += $"{curCount}{curChar}";
                curChar = seed[i];
                curCount = 1;
            }
        }
        
        result += $"{curCount}{curChar}";
        
        return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.