diff --git a/solution/0557.Reverse Words in a String III/README_EN.md b/solution/0557.Reverse Words in a String III/README_EN.md new file mode 100644 index 0000000000000..f1b69943ea204 --- /dev/null +++ b/solution/0557.Reverse Words in a String III/README_EN.md @@ -0,0 +1,9 @@ +# Reverse Words in a String III + +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" +``` \ No newline at end of file diff --git a/solution/0557.Reverse Words in a String III/Solution.java b/solution/0557.Reverse Words in a String III/Solution.java new file mode 100644 index 0000000000000..7b64a1d18f6a7 --- /dev/null +++ b/solution/0557.Reverse Words in a String III/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public String reverseWords(String s) { + String flag = " "; + StringBuilder result = new StringBuilder(); + for (String temp : s.split(flag)) { + for (int i = temp.length() - 1; i >= 0; i--) { + result.append(temp.charAt(i)); + } + result.append(flag); + } + return result.toString().substring(0, s.length()); + } +} \ No newline at end of file