Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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.
"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 <= 200s 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.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"is2 sentence4 This1 a3"
"Myself2 Me1 I4 and3"
check-if-numbers-are-ascending-in-a-sentence)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1859: Sorting the Sentence
class Solution {
public String sortSentence(String s) {
String[] ws = s.split(" ");
int n = ws.length;
String[] ans = new String[n];
for (int i = 0; i < n; ++i) {
String w = ws[i];
ans[w.charAt(w.length() - 1) - '1'] = w.substring(0, w.length() - 1);
}
return String.join(" ", ans);
}
}
// Accepted solution for LeetCode #1859: Sorting the Sentence
func sortSentence(s string) string {
ws := strings.Split(s, " ")
ans := make([]string, len(ws))
for _, w := range ws {
ans[w[len(w)-1]-'1'] = w[:len(w)-1]
}
return strings.Join(ans, " ")
}
# Accepted solution for LeetCode #1859: Sorting the Sentence
class Solution:
def sortSentence(self, s: str) -> str:
ws = s.split()
ans = [None] * len(ws)
for w in ws:
ans[int(w[-1]) - 1] = w[:-1]
return " ".join(ans)
// Accepted solution for LeetCode #1859: Sorting the Sentence
struct Solution;
impl Solution {
fn sort_sentence(s: String) -> String {
let mut pairs: Vec<(String, String)> = s
.split_whitespace()
.map(|s| {
let n = s.len();
let p = s[n - 1..].to_string();
let word = s[0..n - 1].to_string();
(p, word)
})
.collect();
pairs.sort_unstable();
let words: Vec<String> = pairs.into_iter().map(|(_, word)| word).collect();
words.join(" ")
}
}
#[test]
fn test() {
let s = "is2 sentence4 This1 a3".to_string();
let res = "This is a sentence".to_string();
assert_eq!(Solution::sort_sentence(s), res);
let s = "Myself2 Me1 I4 and3".to_string();
let res = "Me Myself and I".to_string();
assert_eq!(Solution::sort_sentence(s), res);
}
// Accepted solution for LeetCode #1859: Sorting the Sentence
function sortSentence(s: string): string {
const ws = s.split(' ');
const ans = Array(ws.length);
for (const w of ws) {
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
}
return ans.join(' ');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.