1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
public static String countAndSay(int n) {
String result="1";
for(int i=1;i<n;i++)
{
char temp=result.charAt(0);
int num=0;
String rs=new String();
for(int j=0;j<result.length();j++)
{
if(result.charAt(j)==temp) num++;
if(result.charAt(j)!=temp)
{
rs=rs+num+result.charAt(j-1);
temp=result.charAt(j);
j--;
num=0;
}
}
//System.out.println("dfsd"+rs);
if(num==result.length()){
//result="";
result=""+(result.length())+temp;
}else {
result=rs+num+result.charAt(result.length()-1);
}
}
return result;
}