Java Solution using String Builder


  • 0
    Z

    '''
    public class ReverseWord {

    /** Input: "Let's take LeetCode contest" **/
    /** Output: "s'teL ekat edoCteeL tsetnoc" **/
    
    public static void main(String[] args) {
    	ReverseWord r = new ReverseWord();
    	String str = "Let's take LeetCode contest";
        System.out.println(r.reverseWords(str));
    }
    
    public String reverseWords(String s) {
    /**Split each word and insert into array**/
    String [] token = s.split(" ");
    /**iterate the array and reverse each word,and appen it to the new String builder**/
    StringBuilder sb=new StringBuilder();
    for(String str:token)
       sb.append(new StringBuilder(str).reverse()+" ");
    	
    	
      return sb.toString().trim();
    	
    	
    }
    

    }

    '''


Log in to reply
 

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