Java StringBuilder+String.join()


  • 1
    S

    By using String.join() method, we don't need to use another StringBuilder

    public class Solution {
        public String reverseWords(String s) {
            //check if input is valid
            if (s == null || s.length() == 0)
                return "";
                
            String[] str = s.split(" ");
    
            for (int i = 0; i < str.length; i++) {
                StringBuilder temp = new StringBuilder(str[i]);
                str[i] = temp.reverse().toString();
            }
            
            return String.join(" ", str);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.