Class Solution

java.lang.Object
g0001_0100.s0038_count_and_say.Solution

public class Solution extends Object
38 - Count and Say.<p>Medium</p> <p>The <strong>count-and-say</strong> sequence is a sequence of digit strings defined by the recursive formula:</p> <ul> <li><code>countAndSay(1) = &quot;1&quot;</code></li> <li><code>countAndSay(n)</code> is the way you would &ldquo;say&rdquo; the digit string from <code>countAndSay(n-1)</code>, which is then converted into a different digit string.</li> </ul> <p>To determine how you &ldquo;say&rdquo; a digit string, split it into the <strong>minimal</strong> number of groups so that each group is a contiguous section all of the <strong>same character.</strong> Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying.</p> <p>For example, the saying and conversion for digit string <code>&quot;3322251&quot;</code>:</p> <p><img src="https://assets.leetcode.com/uploads/2020/10/23/countandsay.jpg" alt="" /></p> <p>Given a positive integer <code>n</code>, return <em>the</em> <code>nth</code> <em>term of the <strong>count-and-say</strong> sequence</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 1</p> <p><strong>Output:</strong> &ldquo;1&rdquo;</p> <p><strong>Explanation:</strong> This is the base case.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 4</p> <p><strong>Output:</strong> &ldquo;1211&rdquo;</p> <p><strong>Explanation:</strong></p> <pre><code> countAndSay(1) = &quot;1&quot; countAndSay(2) = say &quot;1&quot; = one 1 = &quot;11&quot; countAndSay(3) = say &quot;11&quot; = two 1's = &quot;21&quot; countAndSay(4) = say &quot;21&quot; = one 2 + one 1 = &quot;12&quot; + &quot;11&quot; = &quot;1211&quot; </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 30</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countAndSay

      public String countAndSay(int n)