Find the n’th term in Look-and-say (Or Count and Say) Sequence. The look-and-say sequence is the sequence of below integers:
1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, …
How is above sequence generated?
n’th term in 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
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 previous term, we scan the previous term. While scanning a term, we simply keep track of count of all consecutive characters. For sequence of same characters, we append the count followed by character to generate the next term.
Below is implementation of 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 sequencestring 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 does'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 programint main(){ int N = 3; cout << countnndSay(N) << endl; return 0;} |
Java
// Java program to find n'th// term in look and say sequenceclass 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 // does't match if (arr[j] != arr[j - 1]) { // Append count of // str[j-1] to temp tmp += cnt + 0; // 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 |
C#
// C# program to find n'th// term in look and say sequenceusing 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 // does't match if (arr[j] != arr[j - 1]) { // Append count of // str[j-1] to temp tmp += cnt + 0; // 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 |
Python3
# Python 3 program to find# n'th term in look and# say sequence# Returns n'th term in# look-and-say sequencedef 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 # does't match if (s[j] != s[j - 1]): # Append count of # str[j-1] to temp tmp += str(cnt + 0) # 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 CodeN = 3print(countnndSay(N))# This code is contributed# by ChitraNayal |
PHP
<?php// PHP program to find// n'th term in look// and say sequence// Returns n'th term in// look-and-say sequencefunction 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 // does'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?> |
Javascript
<script>// Javascript program to find n'th// term in look and say sequence // Returns n'th term in// look-and-say sequencefunction 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 // does't match if (arr[j] != arr[j - 1]) { // Append count of // str[j-1] to temp tmp += cnt + 0; // 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 Codelet N = 3;document.write(countnndSay(N));// This code is contributed by avanitrachhadiya2155</script> |
Output:
21
Thanks to Utkarsh for suggesting the above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA



