Let's make this one less mysterious. For 1451. Rearrange Words in a Sentence, the solution in this repository is mainly a greedy solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are string_split2, join, arrangeWords.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 188 ms, faster than 25.00% of C++ online submissions for Rearrange Words in a Sentence.
02//Memory Usage: 20.1 MB, less than 100.00% of C++ online submissions for Rearrange Words in a Sentence.
03class Solution {
04public:
05 std::vector<std::string> string_split2(std::string str)
06 {
07 // Used to split string around spaces.
08 std::istringstream ss(str);
09 std::vector<std::string> result;
10 std::string word;
11
12 //need to check whether the reading success before using it
13 while(ss >> word){
14 result.push_back(word);
15 }
16
17 return result;
18 }
19
20 template <typename Iter>
21 std::string join(Iter begin, Iter end, std::string const& separator)
22 {
23 std::ostringstream result;
24 result.precision(2); //for floating point
25 if (begin != end)
26 result << *begin++;
27 while (begin != end)
28 //std::fixed : for floating point
29 result << std::fixed << separator << *begin++;
30 return result.str();
31 }
32
33 string arrangeWords(string text) {
34 vector<string> tokens = string_split2(text);
35
36 if(tokens.size() <= 1) return text;
37
38 tokens[0][0] += ('a' - 'A');
39
40 stable_sort(tokens.begin(), tokens.end(),
41 [](const string& a, const string& b){
42 return a.size() < b.size();
43 });
44
45 tokens[0][0] += ('A'-'a');
46
47 string ans = "";
48
49 ans = join(tokens.begin(), tokens.end(), " ");
50
51 return ans;
52 }
53};
Cost