🔥 Day 45 of #100DaysOfCode 🔥 💡 Problem: Length of Last Word ✨ Approach: Trimmed the trailing spaces and used the power of lastIndexOf(' ') to pinpoint the final space — subtracting positions gives the length of the last word. Simple, clean, and blazing fast ⚡ ⚡ Complexity: Time Complexity: O(n) — linear scan through the string. Space Complexity: O(1) — no extra memory used. 📊 Result: ✔️ Accepted (60/60 test cases passed) ⚡ Runtime: 0 ms (🚀 Beats 100.00%) 💾 Memory: 41.55 MB (🔥 Beats 91.62%) 🔑 Key Insight: Elegance in code often comes from simplicity — a single line of logic can beat brute force every time 💫 #LeetCode #100DaysOfCode #Java #StringManipulation #ProblemSolving #CodingChallenge #CleanCode #DSA
Solved LeetCode's Length of Last Word problem in Java with simplicity and speed.
More Relevant Posts
-
📌 Day 2/100 – Remove Element (LeetCode 27) 🔹 Problem: Given an integer array nums and a value val, remove all instances of that value in-place and return the new length of the array. The order of elements can be changed. 🔹 Approach: Used the two-pointer technique to efficiently modify the array in-place. One pointer iterates through the array, while the other tracks the position to overwrite non-val elements. Returned the position of the second pointer as the new length. 🔹 Key Learning: Strengthened understanding of in-place array manipulation. Improved logic building for pointer movement and conditional overwriting. Learned how to minimize extra space usage while maintaining readability and clarity. Another small yet powerful step toward mastering array-based problems! 💻 🔥 #100DaysOfCode #LeetCode #Java #ProblemSolving #TwoPointers #DSA #CodingJourney
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 68 String Manipulation Problem 1:- Largest Odd Number in String Task:- Given a numeric string, return the largest-valued odd number (as a substring) or an empty string if none exists. Example: Input: num = "35427" → Output: "35427" My Approach: Started scanning the string from right to left. The first odd digit encountered marks the end of the required substring. Returned the substring from start to that index. Time Complexity:- O(N) Space Complexity:- O(1) Sometimes, it’s not about complex algorithms just a small logical observation can lead to an efficient solution. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #CodeNewbie #StringManipulation #LogicBuilding #CleanCode
To view or add a comment, sign in
-
-
✅ Day 32 of #100DaysOfCode Challenge 📘 LeetCode Problem 108: Convert Sorted Array to Binary Search Tree 🧩 Problem Statement: Given a sorted array, convert it into a height-balanced Binary Search Tree (BST). A height-balanced BST ensures both left and right subtrees of every node differ in height by at most 1. 💡 Simple Idea: Pick the middle element as the root. Elements on the left become the left subtree. Elements on the right become the right subtree. Repeat the same steps recursively. ⚙ Complexity: ⏱ Time: O(n) 💾 Space: O(log n) (recursion stack height) 🌱 Step by step, building balance in both code and logic! #Day32 #100DaysOfCode #LeetCode #Java #DSA #BinarySearchTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 32 of #100DaysOfCode Challenge 📘 LeetCode Problem 108: Convert Sorted Array to Binary Search Tree 🧩 Problem Statement: Given a sorted array, convert it into a height-balanced Binary Search Tree (BST). A height-balanced BST ensures both left and right subtrees of every node differ in height by at most 1. 💡 Simple Idea: Pick the middle element as the root. Elements on the left become the left subtree. Elements on the right become the right subtree. Repeat the same steps recursively. ⚙ Complexity: ⏱ Time: O(n) 💾 Space: O(log n) (recursion stack height) 🌱 Step by step, building balance in both code and logic! #Day32 #100DaysOfCode #LeetCode #Java #DSA #BinarySearchTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
Day 43 of #100DaysOfLeetCode Today’s problem: Find Pivot Index Concepts practiced: Prefix Sum Technique Array Traversal Efficient Space Optimization Approach: The goal is to find an index such that the sum of all elements to its left equals the sum of all elements to its right. 1️⃣ First, calculate the total sum of the array. 2️⃣ Iterate through the array, and at each index: 🔹 Subtract the current element from the right sum. 🔹 Check if left sum equals right sum → if yes, that’s the pivot index! 3️⃣ Update the left sum and continue. #100DaysOfCode #LeetCode #Java #CodingChallenge #ProblemSolving #LearningInPublic #SoftwareEngineering #DSA
To view or add a comment, sign in
-
-
🌟 **LeetCode 643. Maximum Average Subarray I** 🌟 Today I solved a classic sliding window problem — Maximum Average Subarray I. 📊 🔑 **Problem Insight:** We need to find a contiguous subarray of a fixed length `k` that has the highest possible average value. ⚡ **Key Idea:** Instead of recalculating the sum for every subarray from scratch (which would be inefficient), use the **sliding window** technique. - First, calculate the sum of the first `k` elements. - Then, slide the window one element at a time: subtract the element that leaves the window and add the new element that enters it. - Keep track of the **maximum sum** found during this process. 💡 **Important Learning:** The maximum average is simply the `maximum sum / k`. This approach reduces the time complexity from O(n*k) to an optimal **O(n)**, making it highly efficient for large arrays. ✅ **Result:** Problem accepted on LeetCode with a runtime of 0 ms. 🎉 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #SlidingWindow #CodingJourney #Algorithms
To view or add a comment, sign in
-
-
🔹 Day 31: Add Binary (LeetCode #67) 📌 Problem Statement: Given two binary strings a and b, return their sum as a binary string. ✅ My Approach: Simulated binary addition manually using a carry-based approach. Started from the end of both strings, added corresponding bits along with the carry, appended the remainder (sum % 2) to the result, and updated the carry (sum / 2). Finally, reversed the result string to get the correct binary sum. 📊 Complexity: Time Complexity: O(max(n, m)) Space Complexity: O(max(n, m)) ⚡ Submission Stats: Runtime: 1 ms (Beats 99.71%) Memory: 42.45 MB 💡 Reflection: This problem strengthened my understanding of bitwise addition and string manipulation — a perfect mix of logic and implementation precision! ⚙️ #LeetCode #Java #String #Binary #100DaysOfCode #Day31
To view or add a comment, sign in
-
-
✅ Day 32 of #100DaysOfCode Challenge 📘 LeetCode Problem 108: Convert Sorted Array to Binary Search Tree 🧩 Problem Statement: Given a sorted array, convert it into a height-balanced Binary Search Tree (BST). A height-balanced BST ensures both left and right subtrees of every node differ in height by at most 1. 💡 Simple Idea: Pick the middle element as the root. Elements on the left become the left subtree. Elements on the right become the right subtree. Repeat the same steps recursively. ⚙️ Complexity: ⏱️ Time: O(n) 💾 Space: O(log n) (recursion stack height) 🌱 Step by step, building balance in both code and logic! #Day32 #100DaysOfCode #LeetCode #Java #DSA #BinarySearchTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✨ LeetCode 1657 – Determine if Two Strings Are Close Today I solved an interesting string problem that combines character frequency analysis with transformation logic. In this problem, we’re given two strings and need to determine if one can be transformed into the other using two operations: Swap any two existing characters. Transform all occurrences of one existing character into another existing character (and vice versa). To solve this, I analyzed the conditions that make two strings “close.” They must have: The same set of unique characters, and The same frequency distribution of those characters (even if attached to different letters). By counting character frequencies, verifying the presence of the same characters, and comparing sorted frequency arrays, we can efficiently determine if the strings are close. Time Complexity: O(n) Space Complexity: O(1) This problem helped me strengthen my understanding of frequency mapping and string transformations — a great mix of logic and pattern recognition! ⚡ #LeetCode #Java #DSA #ProblemSolving #StringManipulation #CodingJourney #LearningEveryday
To view or add a comment, sign in
-
-
💡 Day 34/100 ✅ Remove Zeros in Decimal Representation Today’s challenge was about removing all the zeros from a given number’s decimal form. For example, 1020030 → 123. It might look simple, but it reinforced the importance of handling edge cases like n = 0 or n = 1000, which can lead to empty strings or parsing errors. 🧠 Key Features: Practiced both String-based and Math-based approaches. Explored the difference between using int and long for large numbers. Strengthened understanding of number-to-string conversions and parsing. ⚙️ Time Complexity: O(d) (where d = number of digits) 💾 Space Complexity: O(d) #Day33Of100 #DSA #Java #ProblemSolving #CodingJourney #100DaysOfCode #LearnEveryday #LogicBuilding
To view or add a comment, sign in
-
Explore related topics
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