Answer: SQUADVOICE Hiring | Off-Campus OA (2023) | Truncate Sentence Problem
Answer · Posted Jun 2026
Approach Split the sentence into words using spaces. Append the first k words into a StringBuilder, inserting spaces between consecutive words. Return the constructed string. Strategy Split the sentence by spaces. Traverse the first k words. Append each word to the answer. Add a space between words. Return the final string. Java Solution class Solution { public String truncateSentence(String s, int k) { String[] words = s.split(" "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ...
The full answer & interview discussion are available to premium members.
Log in Create a free account