Sorting the Sentence

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
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.
LeetCode Problem - 1859
class Solution {
public String sortSentence(String s) {
// Split the input string by space to get individual words
String[] arr = s.split(" ");
// Initialize an array to store sorted words
String[] result = new String[arr.length];
// Iterate through each word in the input string
for (int i=0; i<arr.length; i++){
// Extract the last character of the current word
char temp = arr[i].charAt((arr[i].length())-1);
// Convert the last character to its integer value
int intVal = temp - '0';
// Place the word in its correct position in the result array based on its integer value
result[intVal-1] = arr[i].substring(0, arr[i].length()-1);
}
// Join the sorted words into a single string separated by space and return
return String.join(" ", result);
}
}




