0% found this document useful (0 votes)
60 views

Given A Pattern As Below and An Integer N Your Task Is To Decode It and Print NTH Row of It

The document describes a pattern encoding problem where the task is to decode a given pattern and print the nth row. It provides sample input/output and 5 solutions in different programming languages to solve the problem. The solutions work by recursively building up the pattern string or using maps/arrays to store previously computed values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Given A Pattern As Below and An Integer N Your Task Is To Decode It and Print NTH Row of It

The document describes a pattern encoding problem where the task is to decode a given pattern and print the nth row. It provides sample input/output and 5 solutions in different programming languages to solve the problem. The solutions work by recursively building up the pattern string or using maps/arrays to store previously computed values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Given a pattern as below and an integer n your task is to decode it

and print nth row of it. The pattern follows as :


1
11
21
1211
111221
............

Input:
The first line of input is the number of test cases .  Then T test cases
follow . The first line of each test case is an integer N.

Output:
For each test case print the required nth row of the pattern.

Constraints:
1<=T<=20
1<=N<=20

Example:
Input:
2
2
3
Output:
11
21
Solution-1:
class GFG {

public static String lookandsay(String number){

StringBuilder result= new StringBuilder();


char repeat= number.charAt(0);

number= number.substring(1) + " ";

int times= 1;

for(char actual: number.toCharArray()){

if(actual != repeat){

result.append(times + "" + repeat);

times= 1;

repeat= actual;

}else{

times+= 1;

return result.toString();

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

int loop = sc.nextInt();

while(loop > 0)

int rowNum = sc.nextInt();

String num = "1";

for (int i=2; i <= rowNum; i++)

num = lookandsay(num);

}
System.out.println(num);

loop --;

Solution-2:
public class test{

public String getPattern(int n){


if(n==0)return "invalid value of n";
if(n==1)return "1";
String pattern=getPattern(n-1);
StringBuffer result=new StringBuffer();
char bck=pattern.charAt(0);
int count=1;
for(int i=1;i<pattern.length();i++){
if(pattern.charAt(i)==bck)count++;
else{
result.append(count).append(bck);
bck=pattern.charAt(i);
count=1;
}
}
result.append(count).append(bck);
return result.toString();
}

public static void main (String[] args) {


Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
int n=sc.nextInt();
test obj=new test();
System.out.println(obj.getPattern(n));
}
}
}
Solution-3:
import java.io.*;
class GFG
{
public static void main (String[] args)
{
//code
Scanner scan = new Scanner(System.in);
int testcases = Integer.parseInt(scan.nextLine());
HashMap<Integer, StringBuffer> map = new HashMap<Integer,
StringBuffer>();
StringBuffer result = new StringBuffer("1"), temp = new
StringBuffer("");
map.put(1, result);
for(int i=2;i<=20;i++){
temp = new StringBuffer("");
int l = result.length();
for(int j=0;j<l;){
int val = countSimilar(result.charAt(j), result, (j+1),l);
temp.append(""+val+""+result.charAt(j));
j=j+val;
}
result = temp;
map.put(i, result);
}
// Iterator it = map.entrySet().iterator();
// while(it.hasNext()){
// Map.Entry e = (Map.Entry)it.next();
// System.out.println(e.getKey()+" "+e.getValue());
// }
while(testcases-->0){
int n = Integer.parseInt(scan.nextLine());
System.out.println(map.get(n));
}
}
public static int countSimilar(char prev, StringBuffer result, int start,
int l){
int count = 1;
for(int i=start;i<l;i++){
if(result.charAt(i) == prev){
count++;
}
else{
break;
}
}
return count;
}
}
Solution-4:
import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
public static void main (String[] args) {
//code
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
if(n==1)
{
System.out.println(1);
continue;
}
if(n==2)
{
System.out.println(11);
continue;
}
String str="11";
for(int i=3;i<=n;i++)
{
str+="$";
String tmp="";
int cnt=1;
char a[]=str.toCharArray();
for(int i1=1;i1<a.length;i1++)
{
if(a[i1]!=a[i1-1])
{
tmp+=cnt;
tmp+=a[i1-1];
cnt=1;
}
else
cnt++;
}
str=tmp;
}
System.out.println(str);
}

}
}
Solution-5:
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
for(int i = 0; i<tc; i++) {
int nthRow = sc.nextInt();
System.out.println(pattern(nthRow));
}
}
public static String pattern(int size) {
String[] str = new String[size+1];
str[0] = "1";
for(int i=1; i<=size; i++) {
String subLiteral = str[i-1];
StringBuilder out = new StringBuilder("");
for(int j = 0; j<subLiteral.length(); j++) {
int subCount = 1;
while(j < subLiteral.length()-1 && subLiteral.charAt(j) ==
subLiteral.charAt(j+1)) {
subCount++;
j++;
}

out.append(Integer.toString(subCount)).append(Character.toString(su
bLiteral.charAt(j)));
}
str[i] = out.toString();
}
return str[size-1];
}
}

You might also like