summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-09-17 18:57:32 +0800
committercrupest <crupest@outlook.com>2020-09-17 18:57:32 +0800
commitc5c5a802895048e21a1144b2517fc833514ce910 (patch)
treede0f16a66ae2dfac3c0baaa1552785da41e3e1b4
parent820229e3dae95eee94753ff8e65e4bf60774d0e2 (diff)
downloadsolutions-c5c5a802895048e21a1144b2517fc833514ce910.tar.gz
solutions-c5c5a802895048e21a1144b2517fc833514ce910.tar.bz2
solutions-c5c5a802895048e21a1144b2517fc833514ce910.zip
Add problem 1370 .
-rw-r--r--cpp/1370.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/cpp/1370.cpp b/cpp/1370.cpp
new file mode 100644
index 0000000..9741d48
--- /dev/null
+++ b/cpp/1370.cpp
@@ -0,0 +1,45 @@
+#include <string>
+
+using std::string;
+
+class Solution
+{
+public:
+ string sortString(string s)
+ {
+ int count[26]{0};
+
+ for (auto c : s)
+ {
+ count[c - 'a']++;
+ }
+
+ int total_count = s.size();
+ string result;
+
+ while (total_count)
+ {
+ for (int i = 0; i < 26; i++)
+ {
+ if (count[i])
+ {
+ count[i]--;
+ total_count--;
+ result.push_back(i + 'a');
+ }
+ }
+
+ for (int i = 25; i >= 0; i--)
+ {
+ if (count[i])
+ {
+ count[i]--;
+ total_count--;
+ result.push_back(i + 'a');
+ }
+ }
+ }
+
+ return result;
+ }
+};