1

I am trying to solve a particular leetcode problem but I can't seem to figure out where I am going wrong.

The question is this one:https://leetcode.com/problems/group-anagrams/ and my code is:

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& str) {
        unordered_map<string, vector<int>> map;
        vector<string> newstr = str;
        int size = str.size();
        //pushing those indices of `str` into the map which share common permutation
        for(int i=0; i<size; i++){
            sort(newstr[i].begin(), newstr[i].end());
            map[newstr[i]].push_back(i);
        }

        vector<vector<string>> ans;
        int i=0;
        for(auto itr: map){
            int v_size = itr.second.size();
            for(int j=0; j<v_size; j++)
                //cout<<str[itr.second[j]]<<" "; //this query works perfectly
                ans[i].push_back(str[itr.second[j]]); //erroneous code
            i++;
        }
        return vector<vector<string>>();
        // return ans;
    }
};
2
  • 1
    What makes you think that this code is erroneous? Is your compiler complaining? Commented Mar 14, 2019 at 21:12
  • The ans has the fix. However your code can be improved. Especially, you most likely don't need to deal with indexes. Here is my version: pastebin.com/raw/uARbVh13 Commented Mar 14, 2019 at 21:45

2 Answers 2

5

You are accessing ans[i] without allocating any memory in ans beforehand. This creates a data access that is out of the vector's bounds (0). If you meant to add a new vector to ans, use ans.push_back(). As balki said, you can also remove redundant allocations by allocating all entries upon declaration, like this:

vector<vector<string>> ans { map.size() };

This will make your code work without any additional changes needed (as far as I can see).

Sign up to request clarification or add additional context in comments.

5 Comments

or initialize ans with size of map, like vector<vector<string>> ans(map.size())
@balki that worked. but a few questions: a) if I initialize the size with 10 and if I add 11 elements, would I face any issues? b) if I initialize the size with 10 and after adding 2 elements, if I fetch the size of the vector would it show 10? or 2?
@KaranSingh a) depends on how you add them: If you simply use ans[12], then it's illegal, but if you use ans.push_back(), then it allocates a new entry and adds the value at the end.
@KaranSingh I would seriously recommend reading the C++ documentation of the vector class, to learn what each operation does, and what you have to watch out for when using them.
@KaranSingh I also suggest, for better code clarity, that you convert vector<vector<string>> into some meaningfully named class type. This makes the code much easier to understand and reason about.
1

When you use

ans[i].push_back(str[itr.second[j]]);

i needs to be a valid index. Since you have not added any items to ans before that call, ans is empty and you have an out of bounds index error. Change the line defines ans to initialize it with appropriate size.

Instead of

vector<vector<string>> ans;

use

vector<vector<string>> ans(map.size());

A second option will be to create a std::vector<std::string> in the loop and add it to ans.

for(auto itr: map){
    int v_size = itr.second.size();

    std::vector<std::string> v;
    for(int j=0; j<v_size; j++)
        v.push_back(str[itr.second[j]]);

    ans.push_back(v);
}

4 Comments

in the second option, how are we able to push back a string in s and also able to push back a string in ans when ans is expecting a vector<string> to be pushed into it?
@KaranSingh, str[iter.second[j]] is char. That's why you can push it to s.
@KaranSingh, sorry, my bad. I misunderstood. Let me fix the answer.
@KaranSingh, it's fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.