My Java solution using split() & StringBuilder


  • 0
    E

    It isn't the most optimal solution, but wanted to share to get critiques.

     public String reverseWords(String s)
      {
        String[] st = s.split(" ");
        StringBuilder reversedS = new StringBuilder();
    
        for (int j = 0; j < st.length; j++)
        {
          for (int i = st[j].length() - 1; i >= 0; i--)
          {
            reversedS.append(st[j].charAt(i));
          }
          if (j != st.length - 1) reversedS.append(" ");
        }
    
        return reversedS.toString();
      }
    

Log in to reply
 

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