Day 22/100 – #100DaysOfCode Today I worked on the Longest Common Prefix problem and focused on improving both efficiency and clarity in my approach. Key Takeaways: Start with the first string as the initial prefix Gradually shrink the prefix until it matches all strings Always handle edge cases (empty array / no common prefix) Small optimizations like avoiding unnecessary string creation can improve performance ⚡ Result: Achieved optimal time complexity: O(N * M) Clean and readable solution 100% test cases passed Example: Input: ["flower", "flow", "flight"] Output: "fl" This problem reinforced an important lesson: Sometimes the best solution is not adding complexity, but simplifying step by step. On to Day 23 #CodingJourney #Java #DSA #LeetCode #ProblemSolving #Consistency
Longest Common Prefix Problem Solution in Java
More Relevant Posts
-
🚀 Day 56/100 Today’s problem: String Compression (In-place) 💡 Key Learnings: - Learned how to use Two Pointers (Read & Write) efficiently - Understood in-place modification without extra space - Handled edge cases like counts greater than 9 (e.g., 12 → '1','2') - Improved thinking around grouping consecutive characters 🧠 Approach: Used a read pointer to traverse the array and a write pointer to update the compressed result directly in the same array. ⚡ Time Complexity: O(n) 📦 Space Complexity: O(1) Consistency is the real game 💯 #Day56 #Java #DSA #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 67 of #LeetCode Challenge ✅ Problem Solved: Check If Two String Arrays are Equivalent 💡 What I learned today: • Learned how to compare two string arrays without joining them • Understood how to traverse multiple strings using pointers • Improved handling of indices across arrays and strings • Realized the importance of edge cases to avoid runtime errors 🧠 Approach: • Used four pointers to track positions in both arrays and strings • Compared characters one by one • Moved to the next string when current string ends • Ensured both arrays are fully traversed at the end 📊 Key Takeaway: Efficient solutions avoid extra space — comparing character by character is better than building new strings 🔥 Consistency + small improvements every day = big progress #Day67 #LeetCode #CodingJourney #DSA #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 23 of #128DaysOfCode Today I solved an interesting string problem where one string is shuffled and an extra character is added. 🔍 What I learned: - How to use character ASCII values to simplify problems - Instead of comparing strings directly, we can use sum difference logic - Converting between char and int makes calculations easier and efficient 💡 Key idea: If we add all characters of both strings and subtract, the remaining value gives the extra character. ⚡ This approach is simple, clean, and runs in O(n) time with O(1) space. Consistency is building momentum 🔥 #Java #DSA #CodingJourney
To view or add a comment, sign in
-
-
💻 Day 35 of #LeetCode Journey 🔥 Solved: 19. Remove Nth Node From End of List Today’s problem was all about mastering Linked Lists and understanding the power of the Two Pointer technique. 🔍 Key Idea: Instead of calculating the length, I used a smart approach with fast and slow pointers. Move the fast pointer n steps ahead Then move both pointers together This helps locate the node to remove in a single pass ⚡ Why this approach? Efficient: O(n) time complexity No extra space required Clean and optimal solution 🧠 What I learned: Using a dummy node simplifies edge cases (like removing the head) Two-pointer technique is very powerful in linked list problems 📌 Problem Link: https://lnkd.in/gxXDR-YV #Java #DataStructures #LinkedList #CodingJourney #100DaysOfCode #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 74 — LeetCode Practice 🚀 Today’s problem: Split a String in Balanced Strings 💡 What I learned today: • Understood the concept of balanced strings (equal number of 'L' and 'R') • Learned how to use a counter approach instead of extra space • Practiced greedy logic to split the string at the right moment • Improved my ability to track conditions while iterating through strings 🧠 Key Idea: Maintain a counter → Increment for 'L' Decrement for 'R' Whenever the counter becomes 0, we found a balanced substring ✅ 🔥 Takeaway: Simple logic + careful observation = efficient solution Consistency is the real game changer 💯 #Day74 #LeetCode #DSA #Java #CodingJourney #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 54 of my #100DaysOfCode Journey Today, I solved LeetCode – Sort Array By Parity II Problem Insight: Rearrange the array such that even-indexed positions have even numbers and odd-indexed positions have odd numbers. Approach: • Created a new result array of same size • Used two pointers: evenplace = 0 and oddplace = 1 • Traversed the array once • Placed even numbers at even indices and odd numbers at odd indices • Incremented pointers by 2 to maintain correct positions Time Complexity: O(n) | Space Complexity: O(n) Key Takeaway: Using two pointers for index placement makes the solution clean, efficient, and avoids unnecessary swaps. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 36 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Longest Common Prefix Problem Insight: Find the longest common prefix among an array of strings. If no common prefix exists, return an empty string. Approach: • Assume the first string as the initial prefix • Iterate through the remaining strings • While the current string doesn’t start with the prefix: – Trim the last character of the prefix • Repeat until a common prefix is found or it becomes empty Time Complexity: • O(n × m) — n = number of strings, m = length of prefix Space Complexity: • O(1) — constant extra space Key Learnings: • Reducing the problem step-by-step makes it easier to solve • Shrinking the prefix is often more efficient than building it • Edge case handling (like empty arrays) is crucial Takeaway: Sometimes the best approach isn’t to construct a solution, but to refine it until it fits perfectly. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Strings
To view or add a comment, sign in
-
-
🚀 Day 3 of #LeetCode Challenge 🔍 Problem: Length of Last Word 💡 Approach: *Started from the end of the string and counted characters until a space was found. *First, trimmed extra spaces using trim() *Traversed backward using a loop Counted characters until hitting a space 🧠 Key Concept: Reverse traversal of strings + handling trailing spaces efficiently. 🔥 #Day3 #LeetCode #Java #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 27/100 Days of Code Challenge Today’s problem: Find Peak Element (Leetcode 162) 🔍 What I learned: How to find a peak element efficiently without scanning the entire array Using Binary Search to reduce time complexity from O(n) to O(log n) Understanding how the “slope” of elements helps decide the search direction 🧠 Key Idea: Instead of checking every element, compare the middle element with its neighbor: If nums[mid] < nums[mid + 1] → move right Else → move left ✅ Example: Input: [1, 2, 3, 1] Output: 2 (index of peak element 3) Consistency is key 🔑 — improving problem-solving skills one day at a time! 💪 #Day27 #100DaysOfCode #LeetCode #BinarySearch #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
The Two-Pointer streak continues! Today’s LeetCode Problem: Is Subsequence. After using multiple pointers to sort arrays and move zeroes, I applied the exact same pattern to string manipulation today. The challenge was figuring out if a shorter string (s) is a valid subsequence of a longer string (t) without disturbing the relative order of the characters. Instead of generating all possible subsequences (which would be a massive O(2ⁿ) performance drain), the Two-Pointer approach solves this beautifully in a single pass. Knocking this out in O(n) time and O(1) space is another great reminder of how incredibly versatile this algorithmic pattern is for both arrays and strings. #DSA #Java #LeetCode #IsSebsequenceProblem
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