🚀 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
LeetCode Solution: Longest Common Prefix in Java
More Relevant Posts
-
🚀 Day 48 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Sort Array By Parity Problem Insight: Rearrange the array so that all even numbers come before odd numbers. The order of elements doesn’t matter. Approach: • Used the two-pointer technique (partition logic) • Maintained a pointer j to track the position for even numbers • Traversed the array using i • Whenever an even number is found, swapped it with index j and incremented j • This ensures all even elements move to the front in a single pass Time Complexity: O(n) Space Complexity: O(1) (in-place solution) Key Learnings: • Two-pointer technique is very useful for array partitioning problems • Swapping helps avoid extra space usage • This pattern is similar to problems like moving zeros or segregating positives/negatives Takeaway: Simple logic + optimal approach = clean and efficient solution. Consistency is making these patterns easier to recognize! #DSA #Java #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 33 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Product of Array Except Self Problem Insight: Given an array, return a new array where each element is the product of all other elements except itself, without using division. Approach: • Used prefix (left) product to store multiplication of previous elements • Traversed from right to calculate suffix product • Combined both to get the final answer • Handled cases with zeros without using division Time Complexity: • O(n) Space Complexity: • O(1) (excluding output array) Key Learnings: • Prefix and suffix technique is very useful • Avoiding division helps handle edge cases • Practice improves problem-solving skills Takeaway: Simple patterns like prefix and suffix can solve complex problems easily with practice. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀100 Days of Code Day-26 LeetCode Practice – Remove Duplicates from Sorted Array Solved a classic problem using the Two Pointer Technique 💡 📌 Problem: Given a sorted array, remove duplicates in-place and return the number of unique elements. 🔍 Key Idea: Since the array is sorted, duplicates are adjacent. Using two pointers helps efficiently overwrite duplicates without extra space. ⚡ Complexity: Time → O(n) Space → O(1) 💻 Clean and optimized approach makes this problem a great example of in-place array manipulation! #LeetCode #Java #DataStructures #CodingPractice #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode – Unique Binary Search Trees II Today’s problem was all about generating all structurally unique BSTs for values from 1 to n. At first glance, it feels tricky because it's not just counting trees — we actually need to build every possible tree structure. 💡 Key Insight: Pick each number i as the root Recursively build: Left subtree from [1 ... i-1] Right subtree from [i+1 ... n] Combine every left & right subtree pair 🌳 This is a classic Recursion + Backtracking problem and is closely related to Catalan Numbers. 📌 Example: For n = 3, we get 5 unique BSTs ⚡ What I learned today: How recursion can generate combinations of structures Importance of base case (start > end → null) Combining subproblems effectively 💻 Time Complexity: Approximately O(4ⁿ / √n) Consistency is key — 90 days strong and still going 💪 Next target: 💯 #DSA #Java #LeetCode #Recursion #BinaryTree #CodingJourney #100DaysOfCode
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 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
-
-
Solved: Product of Array Except Self 💡 Key Takeaway: Instead of recalculating product for every index, we can use prefix and suffix products to build the result efficiently. 👉 Approach I followed: - First pass → store left (prefix) product - Second pass → multiply with right (suffix) product - No division used 📊 Time Complexity: O(n) 📦 Space Complexity: O(1) 🔍 What made it interesting: Understanding how left and right contributions combine at each index to avoid redundant calculations. Consistency + clarity is slowly building confidence 💪 #DSA #Java #LeetCode #CodingJourney #BackendDeveloper #SoftwareEngineering
To view or add a comment, sign in
-
Solved one of the most interesting problems : Subarray Sum Equals K 💡 Key Takeaway: Instead of checking every subarray, tracking prefix sums helps directly identify valid subarrays in an optimized way. 👉 Key Insight: If (prefixSum - k) has already appeared, it means we found a subarray with sum = k. 📊 Time Complexity: O(n) 📦 Space Complexity: O(n) 🔍 What made it interesting? Understanding how previously seen sums help us avoid rework and directly count valid subarrays. Every day I realize: Consistency and clarity of concepts matter more than just solving problems. 💪 #DSA #Java #LeetCode #CodingJourney #BackendDeveloper #SoftwareEngineering
To view or add a comment, sign in
-
Day 17 of #50DaysOfLeetCode Today’s problem: Move Zeroes (LeetCode) Problem Statement: Move all 0s to the end of the array while maintaining the relative order of non-zero elements — all done in-place. Approach: Used the Two Pointer Technique to efficiently rearrange elements in a single pass without extra space. Key Insight: Track the position of the next non-zero element and swap only when necessary. 🔹 Complexity: - Time: O(n) - Space: O(1) #LeetCode #Java #DataStructures #CodingJourney #ProblemSolving #100DaysOfCode #DeveloperLife
To view or add a comment, sign in
-
-
💡 Day 55 of LeetCode Problem Solved! 🔧 🌟21. Merge Two Sorted Lists🌟 🔗 Solution Code: https://lnkd.in/gU7m-4wH 🧠 Approach: • Recursive Merge • Checked for null node base cases to identify the ends of the lists. • Recursively compared the current node values of list1 and list2. • Attached the smaller node to point to the recursively merged result of the remaining nodes. ⚡ Key Learning: • Leveraging recursion drastically simplifies Linked List operations, turning complex pointer-splicing logic into an elegant and readable sequence! ⏱️ Complexity: Time: • O(n + m) — where n and m are the lengths of the two lists Space. • O(n + m) — due to the recursion stack #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfCode #CodingJourney #LinkedList #Recursion
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
Really well explained I faced similar confusion while solving this, especially with edge cases. Your breakdown makes it easier to understand. Keep it up.