1859. Sorting the Sentence LeetCode Solution

In this guide, you will get 1859. Sorting the Sentence LeetCode Solution with the best time and space complexity. The solution to Sorting the Sentence problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sorting the Sentence solution in C++
  4. Sorting the Sentence solution in Java
  5. Sorting the Sentence solution in Python
  6. Additional Resources
1859. Sorting the Sentence LeetCode Solution image

Problem Statement of Sorting the Sentence

A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.
A sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence.

For example, the sentence “This is a sentence” can be shuffled as “sentence4 a3 is2 This1” or “is2 sentence4 This1 a3”.

Given a shuffled sentence s containing no more than 9 words, reconstruct and return the original sentence.

Example 1:

Input: s = “is2 sentence4 This1 a3”
Output: “This is a sentence”
Explanation: Sort the words in s to their original positions “This1 is2 a3 sentence4”, then remove the numbers.

Example 2:

Input: s = “Myself2 Me1 I4 and3”
Output: “Me Myself and I”
Explanation: Sort the words in s to their original positions “Me1 Myself2 and3 I4”, then remove the numbers.

Constraints:

2 <= s.length <= 200
s consists of lowercase and uppercase English letters, spaces, and digits from 1 to 9.
The number of words in s is between 1 and 9.
The words in s are separated by a single space.
s contains no leading or trailing spaces.

See also  962. Maximum Width Ramp LeetCode Solution

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(n)

1859. Sorting the Sentence LeetCode Solution in C++

class Solution {
 public:
  string sortSentence(string s) {
    vector<string> words;
    istringstream iss(s);
    string word;

    while (iss >> word)
      words.push_back(word);

    ranges::sort(words, ranges::less(),
                 [](const string& word) { return word.back(); });

    string ans = trim(words[0]);

    for (int i = 1; i < words.size(); ++i)
      ans += " " + trim(words[i]);

    return ans;
  }

 private:
  string trim(const string& s) {
    return s.substr(0, s.length() - 1);
  }
};
/* code provided by PROGIEZ */

1859. Sorting the Sentence LeetCode Solution in Java

class Solution {
  public String sortSentence(String s) {
    String[] words = s.split(" ");

    Arrays.sort(words, (a, b) -> a.charAt(a.length() - 1) - b.charAt(b.length() - 1));

    StringBuilder sb = new StringBuilder(trim(words[0]));

    for (int i = 1; i < words.length; ++i)
      sb.append(" ").append(trim(words[i]));

    return sb.toString();
  }

  private String trim(final String s) {
    return s.substring(0, s.length() - 1);
  }
}
// code provided by PROGIEZ

1859. Sorting the Sentence LeetCode Solution in Python

class Solution:
  def sortSentence(self, s: str) -> str:
    return ' '.join([word[:-1]
                     for word in sorted(s.split(), key=lambda x: x[-1])])
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.