Given a string s, reverse the order of words. Multiple spaces between words should be reduced to a single space. Leading/trailing spaces should be removed.

Input: "the sky is blue" → Output: "blue is sky the"Input: " hello world " → Output: "world hello"

Split by spaces, filter empty strings, reverse the list, join with single space.

class Solution { public String reverseWords(String s) { String[] words = s.trim().split("\\s+"); int lo = 0, hi = words.length - 1; while (lo < hi) { String tmp = words[lo]; words[lo] = words[hi]; words[hi] = tmp; lo++; hi--; } return String.join(" ", words); } }