Cannot pass one of the tests, but seems nothing wrong for the Custom Testcase

  • 1

    My code cannot pass one of the tests after submission. However, when I type the same test case into the Custom Testcase field and run the code, it seems that my code generates the correct result.

    Here is my code:

    class Solution {
        unordered_map<string, int> m;
        /* Calculate the 'distance' between two characters.
           The 'periodic boundary condition' is also considered. E.g. 'a' - 'z' = 1. */
        char distance(char a, char a0)
            int dist = a - a0;
            if(dist > 13) dist -= 26;
            else if(dist <= -13) dist += 26;
            return '0' + dist;
        /* Shift the string then register the shifted string into an unordered_map.
           The return value specifies when the key appears for the first time. */
        int calcInd(string& s)
            string k;
            for(int i = 0; i < s.size(); i++)
                k.push_back(distance(s[i], s[0]));
                int temp = m.size();
                m[k] = temp;
            return m[k];
        vector<vector<string>> groupStrings(vector<string>& strings) {
            vector<int> index(strings.size());
            int i;
            for(i = 0; i < strings.size(); i++)
                index[i] = calcInd(strings[i]);
            vector<vector<string>> result(m.size());
            for(i = 0; i < strings.size(); i++)
            return result;

    The idea of my code is to use the function "calcInd" to shift each string and register the shifted string into an unordered_map. In the main function, the array 'index' stores the size of the map when the key corresponding to the current string first appears. Thus, if two strings are different after shifting, the 'index' of them should be different. We can then use this 'index' to specify the location of each group in the result vector.

    The result of the test case mentioned above is as below.


    My answer and the expected answer are the same except the order of these groups and the order of strings within the same group. Specifically, my answer is:

    [["fpbnsbrkbcyzdmmmoisaa","rbnzendwnoklpyyyauemm"], ["cpjtwqcdwbldwwrryuclcngw","huoybvhibgqibbwwdzhqhslb"],["a","l","i","d"],["fnuqwejouqzrif","aiplrzejplumda"],["js","nw"],["qcpr","eqdf"],["zghmdiaqmfelr","yfglchzpledkq"],["iedda","zvuur"],["dgwlvcyubde","ilbqahdzgij"],["lpt","txb"],["qzq","sbs"],["zkddvitlk","lwpphufxw"],["xbogegswmad","gkxpnpbfvjm"],["mkndeyrh","wuxnoibr"],["llofdjckor","kknecibjnq"],["lebzshcb","ohecvkfe"],["firomjjlidqpsdeqyn","oraxvssurmzybmnzhw"],["dclpiqbypjpfafukqmjnjg","yxgkdlwtkekavapflheieb"],["lbpabjpcmkyivbtgdwhzlxa","wmalmuanxvjtgmerohskwil"]]

    while the expected answer is:


    Can anyone help me figure out why my code cannot pass this test case after submission? Thanks so much!

  • 1

    Since you realize the different orders but say nothing seems wrong, I guess you overlooked the note?

    "Note: For the return value, each inner list's elements must follow the lexicographic order."

Log in to reply

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