A sentence is a list of words separated by single spaces with no leading/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 and rearranging the words in the sentence. For example, "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:
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.
Constraints:
2 <= s.length <= 200
s
consists of lowercase and uppercase English letters, spaces, and digits from 1
to 9
.s
is between 1
and 9
.s
are separated by a single space.s
contains no leading or trailing spaces.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to sort a sentence involves figuring out the correct order by trying every single possible arrangement. We extract all the words from the sentence and then explore every way to put them back together based on the numbers attached to them.
Here's how the algorithm would work step-by-step:
def sort_the_sentence_brute_force(sentence):
words = sentence.split()
word_positions = {}
for word in words:
# Extract the number and the word without the number
position = int(word[-1])
actual_word = word[:-1]
word_positions[position] = actual_word
sorted_words = []
# Arrange the words based on their identified positions
for position_index in range(1, len(words) + 1):
sorted_words.append(word_positions[position_index])
#Join the sorted words to form the sorted sentence
sorted_sentence = " ".join(sorted_words)
return sorted_sentence
The sentence is jumbled, but each word contains a clue about its correct position. We can use these clues to directly place each word in its proper spot, building the sorted sentence efficiently.
Here's how the algorithm would work step-by-step:
def sort_the_sentence(jumbled_sentence):
words = jumbled_sentence.split()
number_of_words = len(words)
sorted_words = [""] * number_of_words
# Place each word in its correct position based on the trailing number.
for word in words:
index = int(word[-1]) - 1
sorted_words[index] = word[:-1]
# Remove the numbers and build the sorted sentence.
result = " ".join(sorted_words)
return result
Case | How to Handle |
---|---|
Empty input string | Return an empty string as there's nothing to sort. |
Input string with only spaces | Trim the string first, then return an empty string if it becomes empty. |
Missing number at the end of one or more words | Return the original potentially malformed string or throw an IllegalArgumentException. |
Words with duplicate position numbers | The last word with the given number should overwrite any previous words with that number, resolving conflicts. |
Out-of-order position numbers (e.g., 1 3 2) | The algorithm should correctly sort based on the numbers regardless of the initial order. |
Large input string (performance considerations) | Utilize efficient string splitting and sorting algorithms to maintain acceptable performance for large inputs. |
Numbers with leading zeros (e.g., word01) | Parse the number as an integer, which automatically removes the leading zeros. |
Input string containing non-ASCII characters | The solution should correctly handle UTF-8 encoded characters. |