🔥🚀With Explanation | 100% | O(N) | O(1) | Java ✅
Approach\n\n\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity: O(N) //N is length of s\n Add your time complexity here, e.g.
# Approach\n![followStep.jpg](https://assets.leetcode.com/users/images/d4a60478-131d-494f-85b8-d50d98e14806_1700285624.0599198.jpeg)\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N) //N is length of s\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public String sortString(String s) {\n        int[] freq = new int[26];\n        int len = s.length();\n\n        for(char c : s.toCharArray()){\n            freq[c-\'a\']++;\n        }    \n\n        StringBuilder sb = new StringBuilder();\n\n        while(len != 0){\n\n            //step 1-2-3\n            for(int i=0; i<26; i++){\n                if(freq[i] > 0){\n                    sb.append((char)(i+\'a\'));\n                    freq[i]--;len--;\n                }\n            }\n\n            //step 4-5-6\n            for(int i=25; i>=0; i--){\n                if(freq[i] > 0){\n                    sb.append((char)(i+\'a\'));\n                    len--;freq[i]--;\n                }\n            }\n        }    \n\n        return sb.toString();\n    }\n}\n```\nIf you are understand please upvote Brother \uD83D\uDE0A
['Java']