Java Solution using String Builder

  • 0

    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";
    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.