Overall, the difficulty was not high, especially the last two problems, which were not as hard as they should have been.
1370. Increasing Decreasing String
Directly simulate the process of constructing the result string. The process of searching in the string can use binary search. Since the original string needs to be updated, a binary search tree data structure is better.
Time complexity: O(N * log N), space complexity: O(N).
classSolution { public: string sortString(string s){ string ans; multiset<char> container; for (char c : s) { container.insert(c); } while (!container.empty()) { char smallest = *container.begin(); container.erase(container.begin()); ans.push_back(smallest); decltype(container.begin()) it; while ((it = container.upper_bound(smallest)) != container.end()) { ans.push_back(*it); smallest = *it; container.erase(it); } if (!container.empty()) { char largest = *(prev(container.end())); container.erase(prev(container.end())); ans.push_back(largest); while ((it = container.lower_bound(largest)) != container.begin()) { ans.push_back(*prev(it)); largest = *prev(it); container.erase(prev(it)); } } } return ans; } };
1371. Find the Longest Substring Containing Vowels in Even Counts
Observation: odd minus odd equals even, and even minus even equals even. So the problem can be transformed into finding the leftmost position with a given odd/even count state. Since there are 5 vowels and each has odd/even parity, there are 32 states in total.