The Wayback Machine - https://web.archive.org/web/20240917225143/https://www.geeksforgeeks.org/look-and-say-sequence/
Open In App

Look-and-Say Sequence

Last Updated : 04 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Medium
62.54%
30.6K

Find the n’th term in Look-and-say (Or Count and Say) Sequence. The look-and-say sequence is the sequence of the below integers: 
1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, … 

How is the above sequence generated? 
n’th term is generated by reading (n-1)’th term.

The first term is "1"
Second term is "11", generated by reading first term as "One 1"
(There is one 1 in previous term)
Third term is "21", generated by reading second term as "Two 1"
Fourth term is "1211", generated by reading third term as "One 2 One 1"
and so on

How to find n’th term? 

Example: 

Input: n = 3
Output: 21
Input: n = 5
Output: 111221
Recommended Practice

The idea is simple, we generate all terms from 1 to n. First, two terms are initialized as “1” and “11”, and all other terms are generated using previous terms. To generate a term using the previous term, we scan the previous term. While scanning a term, we simply keep track of the count of all consecutive characters. For a sequence of the same characters, we append the count followed by the character to generate the next term.

Below is an implementation of the above idea.  

C++
// C++ program to find n'th term in look and say
// sequence
#include <bits/stdc++.h>
using namespace std;

// Returns n'th term in look-and-say sequence
string countnndSay(int n)
{
    // Base cases
    if (n == 1)      return "1";
    if (n == 2)      return "11";

    // Find n'th term by generating all terms from 3 to
    // n-1.  Every term is generated using previous term
    string str = "11"; // Initialize previous term
    for (int i = 3; i<=n; i++)
    {
        // In below for loop, previous character
        // is processed in current iteration. That
        // is why a dummy character is added to make
        // sure that loop runs one extra iteration.
        str += '$';
        int len = str.length();

        int cnt = 1; // Initialize count of matching chars
        string  tmp = ""; // Initialize i'th term in series

        // Process previous term to find the next term
        for (int j = 1; j < len; j++)
        {
            // If current character doesn't match
            if (str[j] != str[j-1])
            {
                // Append count of str[j-1] to temp
                tmp += cnt + '0';

                // Append str[j-1]
                tmp += str[j-1];

                // Reset count
                cnt = 1;
            }

            //  If matches, then increment count of matching
            // characters
            else cnt++;
        }

        // Update str
        str = tmp;
    }

    return str;
}

// Driver program
int main()
{
    int N = 3;
    cout << countnndSay(N) << endl;
    return 0;
}
Java
// Java program to find n'th 
// term in look and say sequence

class GFG 
{

    // Returns n'th term in 
    // look-and-say sequence
    static String countnndSay(int n)
    {
    // Base cases
    if (n == 1)     return "1";
    if (n == 2)     return "11";

    // Find n'th term by generating 
    // all terms from 3 to n-1. 
    // Every term is generated 
    // using previous term
    
    // Initialize previous term
    String str = "11"; 
    for (int i = 3; i <= n; i++)
    {
        // In below for loop, previous 
        // character is processed in 
        // current iteration. That is
        // why a dummy character is 
        // added to make sure that loop
        // runs one extra iteration.
        str += '$';
        int len = str.length();

        int cnt = 1; // Initialize count 
                     // of matching chars
        String tmp = ""; // Initialize i'th 
                         // term in series
        char []arr = str.toCharArray();
        
        // Process previous term
        // to find the next term
        for (int j = 1; j < len; j++)
        {
            // If current character
            // doesn't match
            if (arr[j] != arr[j - 1])
            {
                // Append count of 
                // str[j-1] to temp
                tmp += cnt;

                // Append str[j-1]
                tmp += arr[j - 1];

                // Reset count
                cnt = 1;
            }

            // If matches, then increment 
            // count of matching characters
            else cnt++;
        }

        // Update str
        str = tmp;
    }

    return str;
    }
    
    // Driver Code
    public static void main(String[] args)
    {
        int N = 3;
        System.out.println(countnndSay(N));
    }
}

// This code is contributed 
// by ChitraNayal
Python3
# Python 3 program to find 
# n'th term in look and 
# say sequence

# Returns n'th term in
# look-and-say sequence
def countnndSay(n):
    
    # Base cases
    if (n == 1):
        return "1"
    if (n == 2):
        return "11"

    # Find n'th term by generating 
    # all terms from 3 to n-1. 
    # Every term is generated using
    # previous term
    
    # Initialize previous term
    s = "11" 
    for i in range(3, n + 1):
        
        # In below for loop, 
        # previous character is 
        # processed in current 
        # iteration. That is why
        # a dummy character is 
        # added to make sure that 
        # loop runs one extra iteration.
        s += '$'
        l = len(s)

        cnt = 1 # Initialize count 
                # of matching chars
        tmp = "" # Initialize i'th 
                 # term in series

        # Process previous term to
        # find the next term
        for j in range(1 , l):
            
            # If current character
            # doesn't match
            if (s[j] != s[j - 1]):
                
                # Append count of 
                # str[j-1] to temp
                tmp += str(cnt)

                # Append str[j-1]
                tmp += s[j - 1]

                # Reset count
                cnt = 1
            
            # If matches, then increment 
            # count of matching characters
            else:
                cnt += 1

        # Update str
        s = tmp
    return s;

# Driver Code
N = 3
print(countnndSay(N))

# This code is contributed
# by ChitraNayal
C#
// C# program to find n'th 
// term in look and say sequence
using System;

class GFG 
{

    // Returns n'th term in 
    // look-and-say sequence
    static string countnndSay(int n)
    {
    // Base cases
    if (n == 1)     return "1";
    if (n == 2)     return "11";

    // Find n'th term by generating 
    // all terms from 3 to n-1. 
    // Every term is generated using
    // previous term
    
    // Initialize previous term
    string str = "11"; 
    for (int i = 3; i <= n; i++)
    {
        // In below for loop, previous 
        // character is processed in 
        // current iteration. That is 
        // why a dummy character is 
        // added to make sure that loop
        // runs one extra iteration.
        str += '$';
        int len = str.Length;

        int cnt = 1; // Initialize count of
                     // matching chars
        string tmp = ""; // Initialize i'th 
                         // term in series
        char []arr = str.ToCharArray();
        
        // Process previous term 
        // to find the next term
        for (int j = 1; j < len; j++)
        {
            // If current character
            // doesn't match
            if (arr[j] != arr[j - 1])
            {
                 // Append count of 
                // str[j-1] to temp
                tmp += cnt;

                // Append str[j-1]
                tmp += arr[j - 1];

                // Reset count
                cnt = 1;
            }

            // If matches, then increment 
            // count of matching characters
            else cnt++;
        }

        // Update str
        str = tmp;
    }

    return str;
    }
    
    // Driver Code
    public static void Main() 
    {
        int N = 3;
        Console.Write(countnndSay(N));
    }
}

// This code is contributed
// by ChitraNayal
JavaScript
<script>

// Javascript program to find n'th 
// term in look and say sequence
    
// Returns n'th term in 
// look-and-say sequence
function countnndSay(n)
{
    
    // Base cases
    if (n == 1)     
        return "1";
    if (n == 2)     
        return "11";
  
    // Find n'th term by generating 
    // all terms from 3 to n-1. 
    // Every term is generated 
    // using previous term
      
    // Initialize previous term
    let str = "11"; 
    
    for(let i = 3; i <= n; i++)
    {
        
        // In below for loop, previous 
        // character is processed in 
        // current iteration. That is
        // why a dummy character is 
        // added to make sure that loop
        // runs one extra iteration.
        str += '$';
        let len = str.length;
        
        // Initialize count 
        // of matching chars
        let cnt = 1; 
        
        // Initialize i'th 
        // term in series
        let tmp = ""; 
        let arr = str.split("");
          
        // Process previous term
        // to find the next term
        for(let j = 1; j < len; j++)
        {
            
            // If current character
            // doesn't match
            if (arr[j] != arr[j - 1])
            {
                
                // Append count of 
                // str[j-1] to temp
                tmp += cnt;
  
                // Append str[j-1]
                tmp += arr[j - 1];
  
                // Reset count
                cnt = 1;
            }
  
            // If matches, then increment 
            // count of matching characters
            else cnt++;
        }
  
        // Update str
        str = tmp;
    }
    return str;
}

// Driver Code
let N = 3;

document.write(countnndSay(N));

// This code is contributed by avanitrachhadiya2155

</script>
PHP
<?php 
// PHP program to find 
// n'th term in look 
// and say sequence

// Returns n'th term in
// look-and-say sequence
function countnndSay($n)
{
    // Base cases
    if ($n == 1)     
        return "1";
    if ($n == 2)     
        return "11";

    // Find n'th term by generating 
    // all terms from 3 to n-1. 
    // Every term is generated 
    // using previous term
    
    // Initialize previous term
    $str = "11"; 
    for ($i = 3; 
         $i <= $n; $i++)
    {
        // In below for loop, 
        // previous character is 
        // processed in current 
        // iteration. That is why 
        // a dummy character is 
        // added to make sure that 
        // loop runs one extra iteration.
        $str = $str.'$';
        $len = strlen($str);

        $cnt = 1; // Initialize count of
                  // matching chars
        $tmp = ""; // Initialize i'th 
                   // term in series

        // Process previous term 
        // to find the next term
        for ($j = 1; $j < $len; $j++)
        {
            // If current character
            // doesn't match
            if ($str[$j] != $str[$j - 1])
            {
                // Append count of
                // str[j-1] to temp
                $tmp = $tmp.$cnt + 0;

                // Append str[j-1]
                $tmp = $tmp. $str[$j - 1];

                // Reset count
                $cnt = 1;
            }

            // If matches, then increment 
            // count of matching characters
            else $cnt++;
        }

        // Update str
        $str = $tmp;
    }

    return $str;
}

// Driver Code
$N = 3;
echo countnndSay($N);
return 0;

// This code is contributed 
// by ChitraNayal
?>

Output
21

Time Complexity : O(n2) 

Auxiliary Space : O(1)

Another Approach(Using STL): There is one more idea where we can use unordered_map from c++ stl to track the count of digits. Basic idea is to use a generator function that will generate a string from the previous string. In the count and say function we will iterate over integers from 1 to n-1 and keep updating our result.

C++
#include <bits/stdc++.h>
using namespace std;

// generator function returns int string from prev int
// string e.g. -> it will return '1211' for '21' ( One 2's
// and One 1)
string generator(string str)
{
    string ans = "";

    unordered_map<char, int>
        tempCount; // It is used to count integer sequence

    for (int i = 0; i < str.length() + 1; i++) {
        // when current char is different from prev one we
        // clear the map and update the ans
        if (tempCount.find(str[i]) == tempCount.end()
            && i > 0) {
            auto prev = tempCount.find(str[i - 1]);
            ans += to_string(prev->second) + prev->first;
            tempCount.clear();
        }
        // when current char is same as prev one we increase
        // it's count value
        tempCount[str[i]]++;
    }

    return ans;
}

string countnndSay(int n)
{
    string res = "1"; // res variable keep tracks of string
                      // from 1 to n-1

    // For loop iterates for n-1 time and generate strings
    // in sequence "1" -> "11" -> "21" -> "1211"
    for (int i = 1; i < n; i++) {
        res = generator(res);
    }

    return res;
}

int main()
{

    int N = 3;
    cout << countnndSay(N) << endl;
    return 0;
}
Java
import java.io.*;
import java.util.*;

class GFG {
  // generator function returns int string from prev int
  // string e.g. -> it will return '1211' for '21' ( One 2's
  // and One 1)
  static String generator(String str)
  {
    String ans = "";

    HashMap<Character, Integer>tempCount = new HashMap<>(); // It is used to count integer sequence

    for (int i = 0; i < str.length() + 1; i++) {
      // when current char is different from prev one we
      // clear the map and update the ans
      if (i == str.length()  || tempCount.containsKey(str.charAt(i)) == false && i > 0) {
        ans += String.valueOf(tempCount.get(str.charAt(i-1))) + str.charAt(i-1);
        tempCount.clear();
      }
      // when current char is same as prev one we increase
      // it's count value

      if(i == str.length()){
        tempCount.put(null, 1);
      }
      else{
        if(tempCount.containsKey(str.charAt(i))){
          tempCount.put(str.charAt(i), tempCount.get(str.charAt(i))+1);
        }
        else{
          if(i != str.length())tempCount.put(str.charAt(i), 1);
        }
      }
    }
    return ans;
  }

  static String countnndSay(int n)
  {
    String res = "1"; // res variable keep tracks of string
    // from 1 to n-1

    // For loop iterates for n-1 time and generate strings
    // in sequence "1" -> "11" -> "21" -> "1211"
    for (int i = 1; i < n; i++) {
      res = generator(res);
    }

    return res;
  }

  // Driver Code
  public static void main(String args[])
  {
    int N = 3;
    System.out.println(countnndSay(N));
  }
}

// This code is contributed by shinjanpatra
Python3
def generator(s):
  
    # variable to store the result string
    ans = ""
    
    # use a dictionary to count the integer sequence
    tempCount = {}
    
    # loop through the input string
    for i in range(len(s) + 1):
      
        # when the current character is different from the previous one
        # or the end of the input string has been reached
        # update the result string and clear the dictionary
        if i == len(s) or s[i] not in tempCount and i > 0:
            ans += str(tempCount[s[i-1]]) + s[i-1]
            tempCount.clear()
            
        # when the end of the input string has been reached
        if i == len(s):
            tempCount[None] = 1
        else:
          
            # when the current character is the same as the previous one
            # increase its count value
            if s[i] in tempCount:
                tempCount[s[i]] += 1
            else:
              
                # add the character to the dictionary with a count of 1
                tempCount[s[i]] = 1
                
    # return the result string
    return ans

def countAndSay(n):
  
    # initialize the result string with "1"
    res = "1"
    
    # loop n-1 times to generate the sequence of strings
    for i in range(1, n):
        res = generator(res)
        
    # return the result string
    return res

N = 3

# call the countAndSay function with input N and print the result
print(countAndSay(N))

# This Code is Contributed by chinmaya121221
C#
using System;
using System.Collections.Generic;

class GFG {
  
  // generator function returns int string from prev int
  // string e.g. -> it will return '1211' for '21' ( One 2's
  // and One 1)
  static string generator(string str)
  {
    string ans = "";

    Dictionary<char, int> tempCount = new Dictionary<char, int>(); // It is used to count integer sequence

    for (int i = 0; i < str.Length + 1; i++) {
      
      // when current char is different from prev one we
      // clear the map and update the ans
      if (i == str.Length || !tempCount.ContainsKey(str[i]) && i > 0) {
        ans += tempCount[str[i - 1]].ToString() + str[i - 1];
        tempCount.Clear();
      }
      
      // when current char is same as prev one we increase
      // it's count value
      if(i == str.Length){
        tempCount.Add('\0', 1);
      }
      else{
        if(tempCount.ContainsKey(str[i])){
          tempCount[str[i]]++;
        }
        else{
          if(i != str.Length)tempCount.Add(str[i], 1);
        }
      }
    }
    return ans;
  }

  static string countnndSay(int n)
  {
    string res = "1"; // res variable keep tracks of string
    // from 1 to n-1

    // For loop iterates for n-1 time and generate strings
    // in sequence "1" -> "11" -> "21" -> "1211"
    for (int i = 1; i < n; i++) {
      res = generator(res);
    }

    return res;
  }

  // Driver Code
  public static void Main()
  {
    int N = 3;
    Console.WriteLine(countnndSay(N));
  }
}
JavaScript
// generator function returns the count-and-say string for the previous string
// e.g. it will return '1211' for '21' (One 2's and One 1's)
function generator(str) {
  let ans = "";
  let tempCount = new Map(); // It is used to count integer sequences

  for (let i = 0; i < str.length + 1; i++) {
    // When the current character is different from the previous one,
    // we clear the map and update the ans
    if (!tempCount.has(str[i]) && i > 0) {
      const prev = tempCount.get(str[i - 1]);
      ans += prev + str[i - 1];
      tempCount.clear();
    }

    // When the current character is the same as the previous one,
    // we increase its count value
    tempCount.set(str[i], (tempCount.get(str[i]) || 0) + 1);
  }

  return ans;
}

function countAndSay(n) {
  let res = "1"; // res variable keeps track of strings from 1 to n-1

  // For loop iterates n-1 times and generates strings in sequence
  // "1" -> "11" -> "21" -> "1211"
  for (let i = 1; i < n; i++) {
    res = generator(res);
  }

  return res;
}

const N = 3;
console.log(countAndSay(N));

Output
21

Thanks to Utkarsh and Neeraj for suggesting the above solution.


Using dynamic programming:

The idea behind the dynamic programming solution to this problem is to generate each row of the look-and-say pattern based on the previous row, and store all the intermediate results in a vector of strings. By doing this, we avoid repeating the same calculations for multiple rows and reduce the overall time complexity of the solution.

The intuition behind this approach is that the look-and-say pattern is self-referential, meaning that each row can be generated from the previous row by counting the consecutive occurrences of each character. Therefore, if we have the ‘i’th row of the look-and-say pattern, we can generate the ‘i+1’th row by counting the consecutive occurrences of each character in the ‘i’th row.

Follow the steps to implement the above approach:

1. Initialize a vector of strings to store the intermediate results. Set the first element of the vector to “1”, which represents the first row of the look-and-say pattern.

2. Use a for loop to generate each row of the look-and-say pattern, starting from the second row (i = 1) up to the nth row (i < n).

3. For each iteration of the loop, initialize an empty string temp to store the next row of the look-and-say pattern.

4. Use a for loop to iterate through the current row of the look-and-say pattern, stored in the i-1th element of the vector.

5. Within the inner for loop, use a while loop to count the number of consecutive occurrences of the same character.

6. After the while loop, append the count of consecutive occurrences and the current character to the temp string.

7. After the inner for loop, add the temp string to the vector of strings as the ith element, which represents the next row of the look-and-say pattern.

8. After the outer for loop, return the nth element of the vector of strings, which represents the nth row of the look-and-say pattern

Below is the implementation of the above approach

C++
// C++ code to implement the above approach
#include <iostream>
#include <vector>

using namespace std;

// Function to generate the nth row of the look-and-say
// pattern

string generateNthRow(int n)
{
  //vector to store all the intermediary results
    vector<string> dp(n + 1);
  //initialization
    dp[1] = "1";
    for (int i = 2; i <= n; i++) {
        string prev = dp[i - 1];
        string curr = "";
        for (int j = 0; j < prev.size(); j++) {
            int count = 1;
            while (j + 1 < prev.size()
                   && prev[j] == prev[j + 1]) {
                count++;
                j++;
            }
            curr += to_string(count) + prev[j];
        }
        dp[i] = curr;
    }
    return dp[n];
}

int main()
{
    int n = 3;
    cout << generateNthRow(n) << endl;
    return 0;
}
//This code is contributed by Veerendra Singh Rajpoot
Java
// Java program for the above approach
import java.util.*;

public class Main {
  public static String generateNthRow(int n) 
  {

    // vector to store all the intermediary results
    List<String> dp = new ArrayList<>(n + 1);

    // initialization
    dp.add("1");
    for (int i = 2; i <= n; i++) {
      String prev = dp.get(i - 1 - 1);
      String curr = "";
      for (int j = 0; j < prev.length(); j++) {
        int count = 1;
        while (j + 1 < prev.length() && prev.charAt(j) == prev.charAt(j + 1)) {
          count++;
          j++;
        }
        curr += Integer.toString(count) + prev.charAt(j);
      }
      dp.add(curr);
    }
    return dp.get(n - 1);
  }

  public static void main(String[] args) {
    int n = 3;
    System.out.println(generateNthRow(n));
  }
}

// This code is contributed by codebraxnzt
Python3
# Function to generate the nth row of the look-and-say pattern
def generateNthRow(n):
    # List to store all the intermediary results
    dp = [''] * (n + 1)
    # Initialization
    dp[1] = '1'
    for i in range(2, n + 1):
        prev = dp[i - 1]
        curr = ''
        j = 0
        while j < len(prev):
            count = 1
            while j + 1 < len(prev) and prev[j] == prev[j + 1]:
                count += 1
                j += 1
            curr += str(count) + prev[j]
            j += 1
        dp[i] = curr
    return dp[n]

# Example usage
n = 3
print(generateNthRow(n))
C#
using System;
using System.Collections.Generic;

public class Program
{
    public static string GenerateNthRow(int n)
    {
        // Vector to store all the intermediary results
        List<string> dp = new List<string>(n + 1);

        // Initialization
        dp.Add("1");

        for (int i = 2; i <= n; i++)
        {
            // Get the previous row of the sequence
            string prev = dp[i - 1 - 1];

            // Create the current row of the sequence
            string curr = "";
            for (int j = 0; j < prev.Length; j++)
            {
                int count = 1;

                // Count the number of consecutive digits in the previous row
                while (j + 1 < prev.Length && prev[j] == prev[j + 1])
                {
                    count++;
                    j++;
                }

                // Add the count and digit to the current row
                curr += count.ToString() + prev[j];
            }

            // Add the current row to the list of intermediary results
            dp.Add(curr);
        }

        // Return the Nth row of the sequence
        return dp[n - 1];
    }

  // Driver code
    public static void Main(string[] args)
    {
        int n = 3;
        Console.WriteLine(GenerateNthRow(n));
    }
}
JavaScript
// JavaScript program for the above approach
function generateNthRow(n) {

// array to store all the intermediary results
let dp = ["1"];

// initialization
for (let i = 2; i <= n; i++) {
let prev = dp[i - 1 - 1];
let curr = "";
for (let j = 0; j < prev.length; j++) {
let count = 1;
while (j + 1 < prev.length && prev.charAt(j) == prev.charAt(j + 1)) {
count++;
j++;
}
curr += count.toString() + prev.charAt(j);
}
dp.push(curr);
}
return dp[n - 1];
}

// Driver Code
let n = 3;
console.log(generateNthRow(n));

Output
21

Explanation of the above code: here we are using a vector of strings dp to store the generated rows of the look-and-say pattern. We initialize the first element of the dp vector with the string “1”.

In the for loop, we iterate from the 2nd row to the nth row and generate each row based on the previous row. We use a nested for loop to iterate over each character of the previous row. The outer for loop keeps track of the current character, and the inner while loop counts the number of consecutive occurrences of the same character. After counting the number of occurrences, we add the count and the current character to the curr string, which represents the current row of the look-and-say pattern. Finally, we update the dp[i] with the curr string.

After the loop, we return the nth row stored in dp[n]

Time Complexity :O(n*m) where n is the number of rows to generate, and m is the maximum length of a row in the look-and-say pattern. 

space complexity : O(n * m), where n is the number of rows to generate and m is the maximum length of a row in the look-and-say pattern. 

Another Approach(Using Stacks):

Intuition:
the problem is simpler if we understand it as just feeding the output of the counting function into itself n times.

f(1) => 11 // base case
f(11) => 21
f(21) => 1211
f(1211) => 111221
.
.
.
n times

each time we are passing a number into the function we are just counting the number of same digits in a sequence and replacing the digit with the count and the digit

eg 33333 -> there are 5 threes so we return 53(count + digit)

Approach:
we use a stack to count the digits.
if the stack is empty we push to the stack
if the current digit is same as the digit on the top of the stack then we push to the stach.
if the current digit is not the same then we get the length of the stack and the digit in top of the stack return the len + digit as a string, empty the stack and push the current element to the top of the stack

we do this n times. each time the output of the function will be the input to the next iteration.

C++
// C++ code to implement the above approach
#include <bits/stdc++.h>

using namespace std;

// Function to generate the nth row of the look-and-say
// pattern

string countAndSay(int n)
{
    if (n == 1)
        return "1";

    std::string ret{ "" };
    std::string strToCount{ countAndSay(
        n - 1) }; // we need to count and say the n-1th term
    std::stack<char> stack;

    for (int i{ 0 }; i <= strToCount.length(); ++i) {
        // if we're at the end OR we find a num that's
        // different... we add the length of the stack to ret
        // and also the num we've been counting then, we
        // empty the stack to count the next number
        if (i == strToCount.length()
            || !stack.empty()
                   && stack.top() != strToCount[i]) {
            std::stringstream ss;
            std::string toAdd{ "" };
            ss << stack.size();
            ss >> toAdd;
            ret += toAdd;
            ret += stack.top();
            while (!stack.empty())
                stack.pop();
        }
        if (i != strToCount.length())
            stack.push(strToCount[i]);
    }
    return ret;
}

int main()
{
    int n = 3;
    cout << countAndSay(n) << endl;
    return 0;
}
Java
/*package whatever //do not write package name here */

import java.io.*;

import java.util.Stack;

public class GFG{
    public static String countAndSay(int n) {
        if (n == 1)
            return "1";

        String strToCount = countAndSay(n - 1);
        StringBuilder ret = new StringBuilder();
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i <= strToCount.length(); ++i) {
            if (i == strToCount.length() || (!stack.empty() && stack.peek() != strToCount.charAt(i))) {
                ret.append(stack.size()).append(stack.peek());
                stack.clear();
            }
            if (i != strToCount.length())
                stack.push(strToCount.charAt(i));
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        int n = 3;
        System.out.println(countAndSay(n));
    }
}
Python3
# Function to generate the nth row of the look-and-say pattern
def count_and_say(n):
    if n == 1:
        return "1"

    ret = ""
    # we need to count and say the n-1th term
    str_to_count = count_and_say(n - 1)
    stack = []

    for i in range(len(str_to_count) + 1):
        # if we're at the end OR we find a num that's different... we add the length of the stack to ret
        # and also the num we've been counting then, we empty the stack to count the next number
        if i == len(str_to_count) or (stack and stack[-1] != str_to_count[i]):
            to_add = str(len(stack)) + stack[-1]
            ret += to_add
            stack.clear()

        if i != len(str_to_count):
            stack.append(str_to_count[i])

    return ret


def main():
    n = 3
    print(count_and_say(n))


if __name__ == "__main__":
    main()


# This code is contributed by shivamgupta310570
C#
// C# code to implement the above approach
using System;
using System.Collections.Generic;

namespace CountAndSay
{
    class Program
    {
        // Function to generate the nth term of the "count and say" sequence
        static string CountAndSay(int n)
        {
            // Base case: return "1" for n = 1
            if (n == 1)
                return "1";

            // String to store the current term being generated
            string ret = "";

            // Recursively calculate the (n-1)th term
            string strToCount = CountAndSay(n - 1);

            // Stack to count consecutive occurrences of the same digit
            Stack<char> stack = new Stack<char>();

            // Loop through the characters of the (n-1)th term
            for (int i = 0; i <= strToCount.Length; ++i)
            {
                // If we're at the end of the string or the stack is not empty and the current character is different from the top of the stack
                if (i == strToCount.Length || (stack.Count > 0 && stack.Peek() != strToCount[i]))
                {
                    // Add the count and digit value to the result
                    ret += stack.Count.ToString() + stack.Peek();

                    // Clear the stack to count the next number
                    stack.Clear();
                }

                // If we're not at the end of the string, push the current character onto the stack
                if (i != strToCount.Length)
                    stack.Push(strToCount[i]);
            }

            // Return the nth term of the "count and say" sequence
            return ret;
        }

        static void Main(string[] args)
        {
            int n = 3;
            
            // Generate and print the 3rd term of the "count and say" sequence
            Console.WriteLine(CountAndSay(n));
        }
    }
}
JavaScript
function countAndSay(n) {
    if (n === 1)
        return "1";

    let strToCount = countAndSay(n - 1);
    let ret = "";
    let stack = [];

    for (let i = 0; i <= strToCount.length; ++i) {
        if (i === strToCount.length || (stack.length !== 0 && stack[stack.length - 1] !== strToCount[i])) {
            ret += stack.length + stack[stack.length - 1];
            stack = [];
        }
        if (i !== strToCount.length)
            stack.push(strToCount[i]);
    }
    return ret;
}

let n = 3;
console.log(countAndSay(n));

Output
21

Time complexity: O(kn),since we are calling the function n times and for k length of the string
Auxiliary Space: O(kn),stack space needed.



Similar Reads

Difference between FCFS and C-LOOK disk scheduling algorithm
1. FCFS Disk Scheduling Algorithm FCFS stands for First Come First Serve, this algorithm entertains the task in the order they arrived in the disk queue. It is the simplest and easy to understand disk scheduling algorithm. In this, the head or pointer moves in the direction in which the task arrives and moves till all request is served. However, th
3 min read
Difference between C-LOOK and C-SCAN Disk Scheduling Algorithm
C-LOOK Disk Scheduling Algorithm C-LOOK is the modified version of both LOOK and C-scan algorithms. In this algorithm, the head starts from the first request in one direction and moves towards the last request at another end, serving all request in between. After reaching the last request in one end, the head jumps in another direction and move tow
3 min read
Compute the parity of a number using XOR and table look-up
Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has “odd parity”, if it contains odd number of 1-bits and is “even parity” if it contains even number of 1-bits. 1 --&gt; parity of the set is odd 0 --&gt; parity of the set is even Examples: Input : 254 Output : Odd Parity Explanation : Binary of 254 is 11
9 min read
How to Implement Reverse DNS Look Up Cache?
Reverse DNS look up is using an internet IP address to find a domain name. For example, if you type 74.125.200.106 in browser, it automatically redirects to google.in. How to implement Reverse DNS Look Up cache? Following are the operations needed from cache: Add an IP address to URL Mapping in cache.Find URL for a given IP address. One solution is
15+ min read
How to Implement Forward DNS Look Up Cache?
We have discussed implementation of Reverse DNS Look Up Cache. Forward DNS look up is getting IP address for a given domain name typed in the web browser. The cache should do the following operations : 1. Add a mapping from URL to IP address 2. Find IP address for a given URL. There are a few changes from reverse DNS look up cache that we need to i
13 min read
Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
Given a sequence of integers of even length 'n', the task is to find the minimum number of operations required to convert the sequence to follow the rule a[i]=a[i+2] where 'i' is the index. The operation here is to replace any element of the sequence with any element. Examples: Input : n=4 ; Array : 3, 1, 3, 2 Output : 1 If we change the last eleme
11 min read
Convert an unbalanced bracket sequence to a balanced sequence
Given an unbalanced bracket sequence of '(' and ')', convert it into a balanced sequence by adding the minimum number of '(' at the beginning of the string and ')' at the end of the string. Examples: Input: str = ")))()" Output: "((()))()" Input: str = "())())(())())" Output: "(((())())(())())" Method 1: Approach: Initialize an empty stack.Iterate
13 min read
Find original sequence from Array containing the sequence merged many times in order
Given a number N and an array arr[] that consist of merging N length sequence of distinct integers any number of times maintaining the relative order of elements in the initial sequence. The task is to find the initial sequence of length N maintaining the right order. Examples: Input: N = 4, arr[] = {1, 13, 1, 24, 13, 24, 2, 2}Output: 1 13 24 2 Exp
10 min read
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
Given a string S of valid parentheses sequence of length N and an even integer K, the task is to find the valid parentheses sequence of length K which is also a subsequence of the given string. Note: There can be more than one valid sequence, print any of them. Examples: Input: S = "()()()", K = 4Output: ()()Explanation:The string "()()" is a subse
7 min read
Generate a sequence X from given sequence Y such that Yi = gcd(X1, X2 , ... , Xi)
Given a sequence Y of size N where: Yi = gcd(X1, X2, X3, . . ., Xi ) of some sequence X. The task is to find such a sequence X if any such X is possible. Note: If X exists there can be multiple value possible for X. Generating any one is sufficient. Examples: Input: N = 2, Y = [4, 2]Output: [4, 2]Explanation: Y0 = gcd(X0) = X0 = 4. And gcd(4, 2) =
7 min read
k-th missing element in increasing sequence which is not present in a given sequence
Given two sequences, one is increasing sequence a[] and another a normal sequence b[], find the K-th missing element in the increasing sequence which is not present in the given sequence. If no k-th missing element is there output -1 Examples: Input: a[] = {0, 2, 4, 6, 8, 10, 12, 14, 15}; b[] = {4, 10, 6, 8, 12}; k = 3 Output: 14 Explanation : The
6 min read
Split the array elements into strictly increasing and decreasing sequence
Given an array of N elements. The task is to split the elements into two arrays say a1[] and a2[] such that one contains strictly increasing elements and the other contains strictly decreasing elements and a1.size() + a2.size() = a.size(). If it is not possible to do so, print -1 or else print both the arrays. Note: There can be multiple answers an
7 min read
Sequence with sum K and minimum sum of absolute differences between consecutive elements
Given two integers N and K, the task is to find a sequence of integers of length N such that the sum of all the elements of the sequence is K and the sum of absolute differences between all consecutive elements is minimum. Print this minimized sum. Examples: Input: N = 3, K = 56 Output: 1 The sequence is {19, 19, 18} and the sum of absolute differe
4 min read
Find F(n) when F(i) and F(j) of a sequence are given
Given five integers i, Fi, j, Fj and N. Where Fi and Fj are the ith and jth term of a sequence which follows the Fibonacci recurrence i.e. FN = FN - 1 + FN - 2. The task is to find the Nth term of the original sequence.Examples: Input: i = 3, F3 = 5, j = -1, F-1 = 4, N = 5 Output: 12 Fibonacci sequence can be reconstructed using known values: ...,
9 min read
Count the number of contiguous increasing and decreasing subsequences in a sequence
For a given distinct integer sequence of size N, the task is to count the number of contiguous increasing subsequence and contiguous decreasing subsequence in this sequence.Examples: Input: arr[] = { 80, 50, 60, 70, 40 } Output: 1 2 Explanation: The only one increasing subsequence is (50, 60, 70) and two decreasing subsequences are (80, 50) and (70
9 min read
Count Possible Decodings of a given Digit Sequence in O(N) time and Constant Auxiliary space
Given a digit sequence S, the task is to find the number of possible decodings of the given digit sequence where 1 represents 'A', 2 represents 'B' ... and so on up to 26, where 26 represents 'Z'. Examples: Input: S = "121" Output: 3 The possible decodings are "ABA", "AU", "LA" Input: S = "1234" Output: 3 The possible decodings are "ABCD", "LCD", "
6 min read
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
Given two numbers N and K, where K represents the starting term of the sequence. The task is to find the Nth term of a sequence formed by sum of current term with product of largest and smallest digit of current term, i.e., AN+1 = AN + max(digits of AN) * min(digits of AN) Examples: Input: K = 1, N = 5 Output: 50 Explanation: A1 = 1 A2 = A1 + minDi
6 min read
Length of smallest sequence having sum X and product Y
Given two integers X and Y, the task is to find the length of the smallest sequence consisting of positive integers having sum X and product Y. If no such sequence can be generated, print -1. Examples: Input: X = 5, Y = 5Output: 1Explanation:The smallest possible sequence having sum 5 and product 5 is {5}. Therefore, the required answer is length o
7 min read
Shuffle the given Matrix K times by reversing row and columns alternatively in sequence
Given a matrix arr[][] of size M * N, where M is the number of rows and N is the number of columns, and an integer K. The task is to shuffle this matrix in K steps such that in each step, the rows and columns of the matrix are reversed alternatively i.e start with reversing the 1st row first step, then the 1st column in the second step, then the 2n
7 min read
Generate a sequence with product N such that for every pair of indices (i, j) and i &lt; j, arr[j] is divisible by arr[i]
Given a positive integer N, the task is to generate a sequence say arr[] of maximum length having all elements at least 2 such that the product of all the numbers in the sequence is N and for any pair of indices (i, j) and i &lt; j, arr[j] is divisible by arr[i]. Examples: Input: N = 360Output: Maximum size = 3, final array = {2, 2, 90}Explanation:
11 min read
Find N distinct integers with GCD of sequence as 1 and GCD of each pair greater than 1
Given an integer N, the task is to find a sequence of N distinct positive integers such that the Greatest Common Divisor of the sequence is 1 and GCD of all possible pairs of elements is greater than 1. Input: N = 4Output: 84 60 105 70Explanation: The GCD(84, 60, 105, 70) is 1 and the GCD of all possible pair of elements i.e, {(84, 60), (84, 105),
5 min read
Distribute N in a sequence having K-sized groups of 1, 2, 4 and so on
Given a number N, and an integer K. The task is to distribute N in a sequence such that the first K numbers of the sequence is 20, the next K numbers are 21, and so on such that the sum of the sequence is at most N. Find the largest size of the sequence. Examples: Input: N = 35, K = 5Output: 15Explanation: The sequence is 1 1 1 1 1 2 2 2 2 2 4 4 4
4 min read
MEX of generated sequence of N+1 integers where ith integer is XOR of (i-1) and K
Given two integers N and K, generate a sequence of size N+1 where the ith element is (i-1)⊕K, the task is to find the MEX of this sequence. Here, the MEX of a sequence is the smallest non-negative integer that does not occur in the sequence. Examples: Input: N = 7, K=3Output: 8Explanation: Sequence formed by given N and K is {0⊕3, 1⊕3, 2⊕3, 3⊕3, 4⊕
12 min read
Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays
We are given two arrays, we need to find the longest possible bitonic sequence such that the increasing part must be from the first array and should be a subsequence of the first array. Similarly, the decreasing part of must be from the second array and should be a subsequence of it. Examples: Input : arr1[] = {1, 5, 2, 4, 3, 5}, arr2[] = {8, 6, 4,
10 min read
Sequence and Series in Python
Sequences and series are fundamental concepts in mathematics. A Sequence is an ordered list of numbers following a specific pattern, while a series is the sum of the elements of a sequence. This tutorial will cover arithmetic sequences, geometric sequences, and how to work with them in Python. Arithmetic Sequence:An arithmetic sequence is a sequenc
5 min read
Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array
Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous sequence of 1s. Expected time complexity is O(n) and auxiliary space is O(1). Example: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1} Output: Index 9 Assuming array index starts from 0, replacing 0 with 1 at index 9 causes the maximum continu
9 min read
Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous sequence of 1s. Expected time complexity is O(n) and auxiliary space is O(1). Examples: Input : arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1} Output : Index 9 Assuming array index starts from 0, replacing 0 with 1 at index 9 causes the maximum cont
15+ min read
Juggler Sequence | Set 2 (Using Recursion)
Juggler Sequence is a series of integer number in which the first term starts with a positive integer number a and the remaining terms are generated from the immediate previous term using the below recurrence relation : [Tex]a_{k+1}=\begin{Bmatrix} \lfloor a_{k}^{1/2} \rfloor &amp; for \quad even \quad a_k\\ \lfloor a_{k}^{3/2} \rfloor &amp; for \q
4 min read
Thue-Morse sequence
Thue–Morse sequence, or Prouhet–Thue–Morse sequence, is an infinite binary sequence of 0s and 1s. The sequence is obtained by starting with 0 and successively appending the Boolean complement of the sequence obtained so far. First few steps : Start with 0 Append complement of 0, we get 01 Append complement of 01, we get 0110 Append complement of 01
6 min read
Sum of the sequence 2, 22, 222, .........
Find the sum of the following sequence : 2, 22, 222, ......... to n terms. Examples : Input : 2 Output: 23.99868 Input : 3 Output: 245.98647 A simple solution is to compute terms one by one and add to the result.The above problem can be efficiently solved using the following formula: C/C++ Code // CPP program to find sum of series // 2, 22, 222, ..
3 min read