Reversing Words in a String with Java on LeetCode

🔹 Day 32 – LeetCode Practice Problem: Reverse Words in a String (LeetCode #151) 📌 Problem Statement: Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters separated by at least one space. Return the words in reverse order, joined by a single space — no extra spaces allowed. ✅ My Approach (Java): Used split() to separate words by spaces. Ignored extra spaces using \\s+ in regex. Reversed the array of words and joined them with a single space. Ensured no leading or trailing spaces appear in the final output. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(n) ⚡ Submission Results: Accepted ✅ Runtime: 12 ms (Beats 13.87%) Memory: 45.26 MB (Beats 16.33%) 💡 Reflection: This problem helped me refine my understanding of string manipulation and space handling in Java. Even though performance can be optimized, the logic was clean and easy to follow. #LeetCode #ProblemSolving #Java #DSA

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories