Finding solutions in an easy, understandable and fast way
Intuition\n[Java] Finding solutions in an easy, understandable and fast way\n\n# Approach\nThe goal is to find a solution to the example using only while and fo
# Intuition\n[Java] Finding solutions in an easy, understandable and fast way\n\n# Approach\nThe goal is to find a solution to the example using only while and for operators\n\n# Complexity\n- Time complexity:\nRuntime: 5 ms       Beats :47.73%\nMemory:  42.1 MB    Beats :91.92%\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public String sortString(String s) {\n        int[] arr = new int[27];\n        String str = "abcdefghijklmnopqrstuvwxyz";\n//Find the number of occurrences of each letter in the text S and \n//place it in an array\n        for(int i=0;i<s.length();i++){\n            int num = str.indexOf((s.charAt(i)));\n            arr[++num]=arr[num]+1;\n        }\n\n        int len = s.length();\n        int count=0;\n        StringBuilder ans = new StringBuilder();\n        \n        while(count<len){\n            for(int i=1;i<27;i++){\n                if(arr[i]>0){\n                    ans.append(str.charAt(i - 1));\n                    arr[i]=arr[i]-1;\n                    count++;\n                }\n            }\n            for(int j=26;j>=1;j--){\n                if(arr[j]>0){\n                    ans.append(str.charAt(j - 1));\n                    arr[j]=arr[j]-1;\n                    count++;\n                }\n            }\n        }\n        return new String(ans);\n    }\n}\n```
['String', 'Java']