Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Count And Say Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Count And Say | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Count-And-Say is a sequence of numbers given below:

  • 1
  • 11
  • 21
  • 1211
  • 111221

This sequence starts with 1. 

1 is read as one 1 -> 11 

11 is read as two 1 -> 21

 21 is read as one 2, one 1 -> 1211 

1211 is read as one 1, one 2, two 1 -> 111221

And so on…

GIven a number n, find the nth sequence.

Approach

The approach to this problem is straightforward and simple.

  • The nth sequence will be generated from the (n - 1)th sequence and the (n - 1)th sequence will be generated from the (n - 2)th sequence and so on till n = 1.
  • Eventually, all the sequences from n to 1 will be generated recursively.
  • For n = 1, return the sequence “1”.  
  • To generate the ith sequence from its previous sequence traverse the previous sequence and keep the count of the consecutive element. As soon as two different elements are encountered or the end of the sequence is reached. First, add the count of the element in the current sequence and then the corresponding element.

Analysis

  • Time Complexity: O(n2)
  • Auxiliary Space Complexity: O(n2)

Implementation

C++
string countAndSay (int n) {
   if(n==1) {
       return "1";
   }
   string previousTerm = countAndSay(n - 1);
   int count = 0;
   char prev = '0';
   string ans = "";
   for(int i = 0; i < previousTerm.size(); i++) {
       if (previousTerm[i] == prev) {
           count++;
       } else {
           if (count != 0) {
               ans += count + '0';
               ans += prev;
           } 
           prev = previousTerm[i];
           count = 1;
       }
   }
   if (count != 0) {
       ans += count + '0';
       ans += prev;
   }
   return ans;
}
Java
class Solution {
   String countAndSay (int n) {
       if(n==1) {
           return "1";
       }
       String previousTerm = countAndSay(n - 1); 
       int count = 0;
       char prev = '0';
       String ans = "";
       for(int i = 0; i < previousTerm.length(); i++) {
           if (previousTerm.charAt(i) == prev) {
               count++;
           } else {
               if (count != 0) {
                   ans += count + 0;
                   ans += prev;
               } 
               prev = previousTerm.charAt(i);
               count = 1;
           }
       }
       if (count != 0) {
           ans += count + 0;
           ans += prev;
       }
       return ans;
   }
}
Related Content
Anagrams
Compare Version Numbers
Integer to Roman Numeral
Longest Common Prefix
Longest Palindrome in String
Insert Minimum To Make Palindrome
Reverse Words in String
Roman Numeral to Integer
Substring Search - I
Substring Search - III
Substring Search - II
Substring Search - IV
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet