DSA Day 95/100

DSA Day 95/100

Topic: Strings

1) Reverse Words in String

Medium

Input: s = "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
public String reverseWords(String s) {
        String[] newstring = s.split(" ");
        s = "";
        for(int i=newstring.length-1;i>=0;i--){
            if(newstring[i]!=""){
                  s = s + newstring[i] + " "; 
            }
        }
        return s.trim();
    }

Thank you for reading :)