C++ iteratively build string with zigzag iteration 98% speed, 100% memory
Count characters using an array instead of a map or vector to improve speed. At each step in the iteration go forwards and backwards through the alphabet to add
Count characters using an array instead of a map or vector to improve speed. At each step in the iteration go forwards and backwards through the alphabet to add letters until the whole string is built.\n\n```\nclass Solution {\npublic:\n    string sortString(string s) {\n        int letters[26];\n        fill_n(letters, 26, 0);\n        for (auto& c : s) letters[c-\'a\']++;\n        string ans = "";\n        while (ans.size() < s.size()){\n            for (int i = 0; i < 26; i++)\n                if (letters[i] > 0){\n                    ans += (char)(i+\'a\');\n                    letters[i]--;\n                }\n            for (int i = 25; i >= 0; i--)\n                if (letters[i] > 0){\n                    ans += (char)(i+\'a\');\n                    letters[i]--;\n                }\n        }\n        return ans;\n    }\n};\n```
[]