🚀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
Solved Length of Last Word problem in Java with 0 ms runtime.
More Relevant Posts
-
🚀Day 94/100 #100DaysOfLeetCode 🧩Problem: Reverse Words in a String III✅ 💻Language: Java 🔍Approach: 🔹Split the given string by spaces to separate each word. 🔹For every word, reverse it using a StringBuilder. 🔹Append each reversed word back to the result string, adding spaces between them. 🔹Finally, return the complete reversed word string. 🧠Key Takeaways: 🔹Practiced efficient string manipulation using StringBuilder. 🔹Learned how splitting and reversing operations can be optimized for clarity. 🔹Strengthened understanding of text processing in Java. ⚡Performance: ⏱️Runtime: 4 ms (Beats 86.86%) 💾Memory: 45.49 MB (Beats 47.42%) #100DaysOfLeetCode #Java #CodingJourney #LeetCode #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
Week 8 || Day 2💡 Reversing words in Java — step by step! Today I practiced reversing each word in a sentence using two different approaches: 🔹 Approach 1 — With .reverse() method: Split the sentence using split(" ") to separate words. Used StringBuffer for each word and applied .reverse() directly. Joined the reversed words back with spaces. 🔹 Approach 2 — Without using .reverse(): Again split the string into words. For each word, used a for loop running from the last character to the first. Appended each character manually into a new StringBuffer. Combined the reversed words carefully, avoiding extra spaces.⚡ #Java #StringBuffer #ProgrammingLogic #JavaFullStack
To view or add a comment, sign in
-
💻 Day 45 of #LeetCode Journey 🚀 ✅ Problem: Count and Say 📘 Language: Java 🔹 Status: Accepted (30/30 test cases passed) 🔹 Runtime: 4 ms | Beats 55.38% 🔹 Memory: 41.86 MB 🧠 Concept: This problem is about generating the n-th term in the “count and say” sequence. Each term is built by describing the previous term — count the number of digits and say them in order. 🧩 Approach: Start with "1". For each iteration, use a StringBuilder to construct the next sequence. Track consecutive digits using a counter. Append the count and digit when the sequence changes. 💡 Efficient string manipulation and iteration give optimal performance. 🔥 Every solved problem builds confidence. One step closer to mastering patterns in strings! #Day45 #LeetCode #Java #CodingChallenge #ProblemSolving #CountAndSay #50DaysOfCode
To view or add a comment, sign in
-
-
🧠 Daily LeetCode Grind — Java Edition Today’s challenge: ✅ Container With Most Water (#11 - Medium) 📌 Goal: Given an integer array height[], find two lines that, together with the x-axis, form a container that holds the most water. 📌 Approach: 🔹 Use the Two-Pointer Technique to optimize the search for the maximum area. 🔹 Start with two pointers at both ends of the array and calculate the area at each step. 🔹 Move the pointer pointing to the smaller height inward to maximize potential area. 🔹 Continue until both pointers meet. 🧩 Test Case: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 💡 Key Takeaways: 🔹 Learned to balance area optimization using width × height. 🔹 Reinforced the power of the two-pointer approach for reducing time complexity. 🔹 Strengthened problem-solving speed through mathematical reasoning. 💻 Language: Java 🧠 Complexity: O(n) — single pass using two pointers. #LeetCode #Java #CodingPractice #ProblemSolving #TwoPointer #Algorithm #DeveloperLife #CybernautEdTech #AcceptedSolution
To view or add a comment, sign in
-
-
🚀Day 97/100 #100DaysOfLeetCode 👩💻Problem: Valid Square✅ 💻Language: Java 💡Approach: To check if four given points form a valid square, I calculated all six pairwise distances between the points. 🔹A valid square must have two distinct distances — 4 equal smaller sides and 2 equal longer diagonals. 🔹Used a HashMap to count occurrences of each distance. 🔹If the map has exactly two distinct non-zero distances and the smaller one appears 4 times while the larger appears 2 times, it’s a square! 🧠Key Takeaways: 🔹Strengthened understanding of geometry-based problems. 🔹Reinforced hashing techniques for quick frequency checks. 🔹Improved logic for pairwise comparison and distance calculation. ⚙️Performance: ⏱️Runtime: 2 ms (Beats 27.27%) 💾Memory: 41.91 MB (Beats 22.03%) #100DaysOfLeetCode #Java #CodingChallenge #LeetCode #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀Day 98/100 #100DaysOfLeetCode 🧩Problem: Linked List Cycle✅ 👩💻Language: Java 🧠Approach: Used the Floyd’s Cycle Detection Algorithm (Tortoise and Hare Method). Two pointers move through the list — slow moves one step at a time, while fast moves two steps. If there’s a cycle, both pointers will eventually meet; otherwise, the list ends without intersection. 💡Key Takeaways: 🔹Efficiently detects a cycle using O(1) space. 🔹Demonstrates a classic use of the two-pointer technique. 🔹A must-know algorithm for linked list manipulation and interview prep. ⚙️Performance: ⏱️Runtime: 0 ms (Beats 100.00%) 💾Memory: 44.16 MB (Beats 96.16%) #100DaysOfLeetCode #Java #ProblemSolving #LinkedList #DataStructures #LeetCode #CodingJourney #CodingChallenge #FloydAlgorithm
To view or add a comment, sign in
-
-
🚀 Day 26 of 100 Days of LeetCode 📘 Problem: Search a 2D Matrix 💻 Language: Java ✅ Status: Accepted — Runtime: ⚡ 0 ms (Beats 100%) Today’s problem focused on searching efficiently within a 2D matrix — a great exercise for understanding nested loops, traversal logic, and time optimization. While this version used a simple brute-force approach, it served as a good refresher on matrix iteration patterns before moving on to more optimized binary search techniques in 2D grids. ✨ Key Learnings: Matrix traversal can be intuitive when visualized 🔢 Always consider both brute-force and optimized approaches — understanding both is key to depth of knowledge Writing clean, readable code matters as much as optimizing it Each problem solved is another small brick in the foundation of strong problem-solving skills 💪 #Day26 #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #SoftwareDevelopment #Matrix #KeepLearning #CodeEveryday
To view or add a comment, sign in
-
-
📌 Day 16/100 - Reverse String (LeetCode 344) 🔹 Problem: Reverse a given string in-place — meaning you must modify the original array of characters without using extra space. 🔹 Approach: Used the two-pointer technique — one starting at the beginning and one at the end of the array. Swap characters at both pointers, then move them closer until they meet. Efficient, clean, and runs in linear time without additional memory allocation. 🔹 Key Learnings: In-place algorithms optimize space complexity significantly. The two-pointer pattern is a versatile tool for many array and string problems. Understanding mutable vs immutable structures in Java is crucial for memory efficiency. Sometimes, the simplest logic beats the most complex one. 🧠 “True efficiency lies in simplicity, not complexity.” #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #TwoPointers
To view or add a comment, sign in
-
-
🧠 Daily LeetCode Grind — Java Edition Today’s challenge: ✅ Integer to Roman (#12 - Medium) 📌 Goal: Convert an integer to its equivalent Roman numeral representation using standard Roman numeral rules and subtractive notation. 📌 Approach: 🔹 Use two arrays — one for values and one for their corresponding Roman symbols. 🔹 Iterate from the largest value to the smallest, appending the corresponding Roman symbol while subtracting its value from the number. 🔹 Continue until the entire number is converted. 🔹 Handles subtractive cases like IV (4), IX (9), XL (40), XC (90), CD (400), and CM (900). 🧩 Test Case: Input: num = 3749 Output: "MMMDCCXLIX" 💡 Key Takeaways: 🔹 Strengthened understanding of greedy algorithms. 🔹 Learned efficient symbol mapping and iteration logic. 🔹 Improved clarity on how Roman numeral rules apply in algorithmic form. 💻 Language: Java 🧠 Complexity: O(1) — fixed number of Roman symbols and operations. #LeetCode #Java #CodingPractice #ProblemSolving #GreedyAlgorithm #DSA #DeveloperLife #CybernautEdTech #AcceptedSolution
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development