Java solution with array, beats 100% time and space
\nclass Solution {\n    public String sortString(String s) {\n      int[] counts = new int[26];\n      for (char x : s.toCharArray()) {\n        counts[x-\'a\']
```\nclass Solution {\n    public String sortString(String s) {\n      int[] counts = new int[26];\n      for (char x : s.toCharArray()) {\n        counts[x-\'a\']++;\n      }\n      \n      boolean change = true;\n      StringBuilder sb = new StringBuilder();\n      while (change) {\n        change = false;\n        \n        for (int i = 0; i < 26; i++) {\n          if (counts[i] > 0) {\n            sb.append((char)(\'a\' + i));\n            counts[i]--;\n            change = true;\n          }\n        }\n\n        for (int i = 25; i >= 0; i--) {\n          if (counts[i] > 0) {\n            sb.append((char)(\'a\' + i));\n            counts[i]--;\n            change = true;\n          }\n        }\n      }\n      \n      return sb.toString();\n      \n    }\n}\n```
[]