LC 1374. Generate a String With Characters That Have Odd Counts

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    string generateTheString(int n) {
        string s;
        if(n%2==0){
            while(n-- > 1){
                s.push_back('a');
            }
            s.push_back('b');
            return s;
        }
        while(n--){
            s.push_back('a');
        }
        return s;
    }
};


Dicussion Forum