Reverse Words in a String

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.
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
LeetCode Problem - 151: Link | Click Here
class Solution {
public String reverseWords(String s) {
// Create a StringBuilder to build the reversed string
StringBuilder sb = new StringBuilder();
// Remove extra spaces between words by replacing multiple spaces with a single space
String removeExtraSpace = s.replaceAll("\\s+", " ");
// Split the string into an array of words using space as the delimiter
String[] resultString = removeExtraSpace.split(" ");
// Iterate through the array of words in reverse order and append each word to the StringBuilder
for (int j = resultString.length - 1; j >= 0; j--) {
sb.append(resultString[j]).append(" ");
}
// Convert the StringBuilder to a string and trim any leading or trailing spaces
String reversedString = sb.toString().trim();
// Return the final reversed string
return reversedString;
}
}




