1
string Solution::countAndSay(int n) {

if (n == 1) 
return "1"; 

if (n == 2)
return "11"; 

string str = "11";

for (int i = 3; i<=n; i++) 
{ 
    str += '$';
    int len = str.length(); 

    int cnt = 1; 
    string  tmp = "";

    for (int j = 1; j < len; j++) 
    { 
        if (str[j] != str[j-1]) 
        { 
            tmp += (cnt + '0');
            tmp += str[j-1]; 
            cnt = 1; 
        } 
        else cnt++; 
    } 
    str = tmp; 
} 
return str;

}

//This is the solution of InterviewBit problem count and say.

Problem is that when I write tmp = tmp + (cnt+'0') instead of tmp += (cnt + '0') then compile error: no match for 'operator+' (operand types are 'std::string' {aka 'std::__cxx11::basic_string'} and 'int')

I thought about its associativity problem and try to concanate empty string to some other string and vice versa but both runs without an error. What is the problem then? Thanks in advance!!

0 Answers0