What's wrong with my String ?


  • 0

    I get the following feedback:

    Input: 2
    Output: 11
    Expected: "11"

    (I'm returning a String)


  • 0
    S

    Probably, this code got crash on another case. Can you present your code and explain it?


  • 0

    I modified my code a bit and worked... I don't know why that error appeared before.
    Thanks!


  • 0
    J
    This post is deleted!

  • 0
    J
    Here is my accepted solution. Follow the comments in the initial if condition    
    class Solution {
        	public:
        		string countAndSay(int n) {
        			string str="1";
        			if(n==1||n==2)	
        			{	
        				if(n==1)
        					return str; // return a string here
                                            // cout << str <<endl; gave me the error
        				str=str+"1";
        				if(n==2)
        					return str; // return a string here
        			}
        			else
        			{
        				str="11";
        				for(int k=0;k<n-2;k++)
        				{
        					string temp="";
        					int i=0,count=1;
        					char seq=str[i];
        					for(i=1;i<str.size()+1;i++)
        					{
        						if(str[i]==seq)
        							count=count+1;
        						else
        						{
        							char a=count+48;
        							temp=temp+a+seq;
        							count=1;
        							seq=str[i];
        						}
        					}
        					str=temp;
        				}
        				return str; // return a string here
        			}
        
        		}
        
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.