Ad
  • Custom User Avatar

    The big thing I had in mind was to do an else-if on the second conditional since it will only be valid if the first one fails. Also, prefix increment/decrement is a tiny bit more efficient.

  • Custom User Avatar

    Ideally, you would want to minimize memory allocations and temporary strings. The previous version would make a new string and do a string concatenation on each loop iteration. There's definitely extra work there that could be removed.

    This version reserves memory ahead of time so there's never more than one memory allocation as the string grows. It also eliminates temporary string objects. Each call to push_back() is only copying a char. Also note the single quotes on the second push_back(). That indicates it's a single char ' ' instead of char array { ' ', '\0' }.

    I also added a reference for the input parameter. You don't need a deep copy of the input and it's generally inadvisable to pass in containers by value since a deep copy could be expensive.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    You could also call the member function "reserve" on the result vector before doing the unique_copy algorithm. This would mean one heap allocation instead of an unknown number of allocations as the vector grows in size.