C++ super clean code using stringstream and getline()

  • 3
    string encode(vector<string>& strs) {
        string res="";
        for(string str:strs) res+=str+'\0';
        return res;
    vector<string> decode(string s) {
        stringstream ss(s);
        vector<string> res;
        string temp;
        while(getline(ss,temp,'\0')) res.push_back(temp);
        return res;

  • 0

    How can this solution work?
    for example, if my string is "\0". this solution cannot work.
    Am I missing anything?

  • 0

    I think all the solutions which are tokenize-baed should not work , because you can always make an example when the delimiter is part of the string in the input vector.

Log in to reply

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