leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

Bug Report for string-encode-and-decode

Open uddivert opened this issue 11 months ago • 0 comments

Bug Report for https://neetcode.io/problems/string-encode-and-decode

Using $ as a delimiter fails. For example this code fails

#include <vector>

class Solution {
public:

    string encode(vector<string>& strs) {
        string result;
        for (string s : strs)
        {
            int size = s.size();
            result += to_string(size) + "p" +s;
        }
        return result;
    }

    vector<string> decode(string s) {
         int index = 0;
         vector <string> result;
        while (index < s.size())
        {
            string ssize;

            while (s.at(index) != 'p') 
            {
                ssize +=s.at(index);
                index ++;
            }

            string val;
            int size = stoi(ssize);
            index ++;

            while (size > 0)
            {
                val +=s.at(index);
                size --;
                index ++;
            }
            result.push_back(val);
        }
        return result;
    }
};

with the following error:

   24 |             while (s.at(index) != '
      |                                   ^
main.cpp:24:35: error: missing terminating ' character
main.cpp: In member function 'std::vector<std::__cxx11::basic_string<char> > Solution::decode(std::string)':
main.cpp:26:26: error: expected primary-expression before 'readInput'
   26 | std::vector<std::string> readInput() {
      |                          ^~~~~~~~~
main.cpp:26:26: error: expected ')' before 'readInput'
main.cpp:24:19: note: to match this '('
   24 |             while (s.at(index) != '
      |                   ^
main.cpp:26:26: error: 'readInput' was not declared in this scope
   26 | std::vector<std::string> readInput() {
      |                          ^~~~~~~~~
main.cpp:37:46: error: a function-definition is not allowed here before '{' token
   37 | std::string parseInput(std::string inputStr) {
      |                                              ^
main.cpp:45:52: error: a function-definition is not allowed here before '{' token
   45 | std::vector<std::string> parseArray(std::string s) {
      |                                                    ^
main.cpp:81:57: error: a function-definition is not allowed here before '{' token
  109 | string printListNoSpaces(std::vector<std::string> list) {
      |                                                         ^
main.cpp:98:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing...
output limit exceeded

but if I change the delimiter from '$' to 'p' then the code works

uddivert avatar Dec 04 '25 07:12 uddivert