Solved Length of Last Word problem in Java with 0 ms runtime.

🚀Day 95/100 #100DaysOfLeetCode 🔍Problem: Length of Last Word✅ 💻Language: Java 🧠Approach: 1️⃣ First, trim the string to remove trailing and leading spaces. 2️⃣ Initialize a counter to 0. 3️⃣ Traverse the string from the end. 4️⃣ Count characters until the first space is encountered. 5️⃣ Return the count as the length of the last word. 💡Key Takeaways: 🔹Trimming the input ensures no trailing spaces interfere. 🔹Backward traversal avoids splitting the entire string. 🔹Simple yet efficient one-pass solution. ⚡Performance: ⏱️Runtime: 0 ms (Beats 100.00%) 💾Memory: 41.58 MB (Beats 91.52%) #100DaysOfLeetCode #Java #CodingJourney #ProblemSolving #LeetCode #CodingChallenge

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories