# Leetcode – Sorting the Sentence Solution

The problem of “Sorting the Sentence” on Leetcode offers an intriguing challenge that involves string manipulation, sorting algorithms, and the ability to creatively solve problems. While this problem may look straightforward at first glance, it’s an excellent opportunity to dig deeper into Python’s rich array of built-in string and list functionalities. In this article, we’ll provide an in-depth analysis of the problem, explore different Python-based approaches, and evaluate their time and space complexities.

## Problem Statement

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 and a single digit i that represents the position of the word in the sentence. You are given a string s representing the sentence and your task is to rearrange the sentence such that all words are sorted in increasing order based on their position in the sentence.

### Example

Input: s = "is2 sentence4 This1 a3"

Output: This is a sentence

## Approach 1: Brute-force String Manipulation

### Algorithm Steps

1. Split the sentence into words.
2. For each word, separate the digit and the actual word.
3. Sort the words based on the extracted digits.
4. Join the sorted words to form the output sentence.

Here’s a Python code snippet for this approach:

def sortSentence(s):
words = s.split(" ")
sorted_words = [None] * len(words)
for word in words:
idx = int(word[-1]) - 1
sorted_words[idx] = word[:-1]
return " ".join(sorted_words)

### Time Complexity

The time complexity is O(n log⁡ n) due to sorting, where n is the number of words.

### Space Complexity

The space complexity is O(n) for storing the sorted words.

## Approach 2: Utilizing Python’s Built-in Sorting Function with Custom Key

Python’s built-in sorted function can sort an array based on a custom key, which, in this case, is the last character of each word.

def sortSentence(s):
words = s.split(" ")
sorted_words = sorted(words, key=lambda x: x[-1])
return " ".join([word[:-1] for word in sorted_words])

### Time Complexity

The time complexity is O(n log⁡ n) due to the use of Python’s built-in sorting function, where nn is the number of words.

### Space Complexity

The space complexity is O(n) due to the sorted words and the list comprehension.

## Approach 3: Bucket Sort

Given the constraints that the digits are from 1 to 9, we can use bucket sort for an even more efficient solution.

def sortSentence(s):
words = s.split(" ")
sorted_words = [None] * len(words)
for word in words:
idx = int(word[-1]) - 1
sorted_words[idx] = word[:-1]
return " ".join(sorted_words)

### Time Complexity

The time complexity is O(n) because bucket sort is a linear time sorting algorithm for this specific case.

### Space Complexity

The space complexity is O(n).

## Unit Testing

It’s crucial to test the code to ensure its robustness and correctness.

def test_sortSentence():
assert sortSentence("is2 sentence4 This1 a3") == "This is a sentence"
assert sortSentence("Myself2 Me1 I4 and3") == "Me Myself and I"
print("All test cases pass")

test_sortSentence()

## Conclusion

The “Sorting the Sentence” problem serves as an excellent playground to explore Python’s built-in capabilities for string manipulation and sorting. It offers different pathways to reach the solution, from brute-force to optimized sorting algorithms. The problem might look simple but offers invaluable lessons in Pythonic programming, algorithmic thinking, and code optimization.