151. Reverse Words in a String | LeetCode | Top Interview 150 | Coding Questions
Published: (December 26, 2025 at 05:03 PM EST)
1 min read
Source: Dev.to
Source: Dev.to
Problem
Solution
class Solution {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
StringBuilder result = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
result.append(words[i]);
if (i != 0) result.append(" ");
}
return result.toString();
}
}