(Easy) Reverse Words in a String III LeetCode
Description
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
Solution
class Solution { public String reverseWords(String s) { if (s==null||s.length()==0){ return ""; } String result =""; String [] stringArr= s.split(" "); for(int i =0; i<stringArr.length;i++){ result = result + Reverse(stringArr[i]); if(i!=stringArr.length-1){ result = result +" "; } } return result; } public String Reverse(String s){ String res = ""; for(int i = s.length()-1; i>=0; i--){ res = res+s.charAt(i); } return res; } }