Day 66/100 🚀 | #100DaysOfDSA Solved LeetCode 14 – Longest Common Prefix today. The task was to find the longest common prefix string among an array of strings. Initially, I couldn’t come up with this optimal approach and first solved it using a brute-force O(n²) method, comparing characters across all strings. Approach (Optimized): Used sorting to simplify the problem. • Sorted the array of strings. • Took the first and last strings after sorting. • Compared characters of both strings until they differ. • The common prefix between these two will be the answer for all strings. This works because after sorting, the most different strings will be at the extremes. Time Complexity: O(n log n + m) (where n = number of strings, m = length of prefix comparison) Space Complexity: O(1) (ignoring sorting space) Key takeaway: Sorting can sometimes reduce multi-string comparison problems to just comparing two extreme cases, making the solution much simpler and efficient. #100DaysOfDSA #LeetCode #DSA #Java #Strings #Sorting #ProblemSolving #Consistency
Optimized LeetCode 14: Longest Common Prefix with Sorting
More Relevant Posts
-
Day 81/100 🚀 | #100DaysOfDSA Solved LeetCode 290 – Word Pattern today. The problem was to check if a pattern string follows the same mapping as words in a given sentence. Approach: Used a HashMap + HashSet to ensure one-to-one mapping. • Split the string s into words • If lengths don’t match → return false • Used a HashMap to map each character in pattern to a word • Used a HashSet to ensure no two characters map to the same word • For each character-word pair: If mapping exists → validate consistency Else → ensure word is not already used, then map it This guarantees a valid bijection between pattern characters and words. Time Complexity: O(n) Space Complexity: O(n) Key takeaway: Whenever a problem involves pattern matching or mapping, always ensure bijection (both directions uniqueness) to avoid conflicts. #100DaysOfDSA #LeetCode #DSA #Java #HashMap #Strings #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 75 of #100DaysOfCode Solved 103. Binary Tree Zigzag Level Order Traversal on LeetCode 🔗 🧠 Key Insight: This is a variation of level order traversal where: 👉 Levels alternate between left → right and right → left ⚙️ Approach (BFS + Direction Toggle): 1️⃣ Use a queue for level order traversal 2️⃣ Maintain a flag (leftToRight or sign) 🔹 true → left → right 🔹 false → right → left 3️⃣ For each level: 🔹 Create a list 🔹 Traverse all nodes in that level 4️⃣ Insert values based on direction: 🔹 If left → right → addLast(val) 🔹 Else → addFirst(val) 5️⃣ Add level list to result 6️⃣ Flip direction: 👉 sign *= -1 or toggle boolean ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #BinaryTree #BFS #Queue #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Day 114 - LeetCode Journey Solved LeetCode 572 – Subtree of Another Tree ✅ This problem focuses on determining whether one binary tree is a subtree of another. A subtree must match both in structure and node values, which makes it more than just a simple value comparison problem. Approach: I used a recursive strategy combining two key steps: Traverse each node of the main tree At every node, check if the subtree starting from that node is identical to the given subRoot For checking identical trees, I implemented a helper function that compares: • Node values • Left subtree • Right subtree If all match, we confirm the subtree exists. Otherwise, we continue searching in the left and right branches of the main tree. Complexity Analysis: • Time Complexity: O(n × m) in the worst case, where n is nodes in root and m is nodes in subRoot • Space Complexity: O(h), due to recursion stack Key Takeaways: • Tree problems often require combining traversal + comparison logic • Breaking problems into helper functions simplifies implementation • Understanding recursion flow is crucial for tree-based questions 🌳 All test cases passed successfully 🎯 #LeetCode #DSA #BinaryTree #Recursion #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 LeetCode Challenge 14/50 💡 Approach: Sort + Two Pointers Brute force checks every triplet — that's O(n³)! Instead, I sorted the array first, then fixed one element and used Two Pointers to find the remaining pair in O(n). Result? O(n²) overall! 🔍 Key Insight: → Sort the array first to enable Two Pointer technique → Fix element at index i, use left & right pointers for the rest → sum < 0 → move left pointer right (need bigger value) → sum > 0 → move right pointer left (need smaller value) → sum = 0 → found a triplet! Skip duplicates carefully 📈 Complexity: ❌ Brute Force → O(n³) Time ✅ Sort + Two Pointer → O(n²) Time, O(1) Space The hardest part wasn't the logic — it was handling duplicates correctly. Details make the difference between a good solution and a great one! 🎯 #LeetCode #DSA #TwoPointers #Java #ADA #PBL2 #LeetCodeChallenge #Day14of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #3Sum
To view or add a comment, sign in
-
-
Day 113 - LeetCode Journey Solved LeetCode 222 – Count Complete Tree Nodes ✅ This problem involves counting the total number of nodes in a complete binary tree, where all levels are fully filled except possibly the last, and nodes are as far left as possible. Approach: I implemented a recursive solution to count nodes. For each node, I recursively calculated the number of nodes in the left and right subtrees and added 1 for the current node. Although this solution works correctly, it follows a straightforward traversal approach and does not fully utilize the properties of a complete binary tree. A more optimized approach can reduce time complexity by comparing left and right subtree heights to detect perfect subtrees and compute their node count directly. Complexity Analysis: • Time Complexity: O(n) • Space Complexity: O(h), where h is the height of the tree (recursion stack) Key Takeaways: • Basic tree traversal (DFS) can solve counting problems effectively • Understanding tree properties can help in optimizing solutions further • Complete binary trees allow more efficient approaches than general trees All test cases passed successfully 🎯 #LeetCode #DSA #BinaryTree #Recursion #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 70/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Reverse Linked List II A neat variation of linked list reversal where only a specific portion of the list is reversed. Problem idea: Reverse a linked list from position left to right, keeping the rest of the list unchanged. Key idea: In-place reversal using pointer manipulation. Why? • We don’t reverse the whole list, only a segment • Need to reconnect the reversed part correctly • Must carefully track boundaries (left and right) How it works: • Use a dummy node to handle edge cases • Move a pointer to the node just before left • Start reversing nodes one by one within the range • Adjust links to insert nodes at the front of the sublist • Reconnect the reversed portion with remaining list Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Partial reversal in linked lists requires precise pointer updates, not extra space. This builds strong intuition for advanced linked list problems. 🔥 Day 70 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #LinkedList #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 70/100 🚀 | #100DaysOfDSA Solved LeetCode 75 – Sort Colors today. The problem was to sort an array containing only 0s, 1s, and 2s in-place without using any built-in sort. Approach: Used the Dutch National Flag Algorithm (3-pointer approach). • Maintained three pointers: left (for 0s), mid (current), and right (for 2s) • Traversed the array using mid If nums[mid] == 0 → swap with left, move both left and mid If nums[mid] == 1 → just move mid If nums[mid] == 2 → swap with right, move right only • Continued until mid > right This ensures all 0s, 1s, and 2s are placed correctly in a single pass. Time Complexity: O(n) Space Complexity: O(1) Key takeaway: Using multiple pointers to partition an array in one pass is a powerful technique for in-place sorting problems. #100DaysOfDSA #LeetCode #DSA #Java #Arrays #TwoPointers #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 63/100 🚀 | #100DaysOfDSA Solved LeetCode 205 – Isomorphic Strings today. The task was to determine if two strings are isomorphic, meaning each character in one string can be replaced to get the other string, maintaining order and uniqueness. Approach: Used two HashMaps to ensure a one-to-one mapping between characters of s and t. • Checked if lengths are equal; if not, return false. • Iterated through both strings simultaneously. • For each character pair (c1, c2): Checked if c1 is already mapped to another character in t. Checked if c2 is already mapped to another character in s. If any mapping violates the isomorphic condition, returned false. • If iteration completes without conflicts, returned true. Time Complexity: O(n) Space Complexity: O(n) Key takeaway: When checking bijective mappings between two sets, always validate both directions to avoid false positives. #100DaysOfDSA #LeetCode #DSA #Java #Strings #HashMap #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔥 Day 60 / 100 – LeetCode Challenge ✅ Solved: 160. Intersection of Two Linked Lists Today’s problem was all about understanding pointer behavior and memory references in linked lists. 💡 Key Insight: Instead of comparing values, we compare node references. If two linked lists intersect, they will share the same tail nodes. 🚀 Approach Used (Optimal): Two pointers (pA, pB) Traverse both lists When one reaches the end, switch to the other list They eventually meet at the intersection node (or null) 🧠 Why it works: Both pointers travel equal distance → LengthA + LengthB, aligning perfectly without extra space. ⏱ Complexity: Time: O(m + n) Space: O(1) 💻 Result: ✔️ Accepted (41/41 test cases) ⚡ Runtime: 1 ms (Beats 99.94%) 📌 Takeaway: Sometimes the smartest solution is not about extra data structures, but about clever traversal. #Day60 #100DaysOfCode #LeetCode #Java #DSA #LinkedList #CodingJourney
To view or add a comment, sign in
-
-
Day 72/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Rotate List A clean linked list manipulation problem that tests pointer handling. Problem idea: Rotate the linked list to the right by k places. Key idea: Convert list into a cycle + break at the right position. Why? • Direct shifting is inefficient • Linked list doesn’t allow random access • Forming a cycle simplifies rotation logic How it works: • Traverse list to find length • Connect tail → head (make it circular) • Reduce k using modulo 👉 k = k % length • Find new tail at (length - k - 1) • Break the cycle to form new head Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Many linked list problems become easier when you temporarily convert structure (like making a cycle) and then restore it. This trick is very powerful in pointer-based problems. 🔥 Day 72 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #LinkedList #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
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