💻 LeetCode 50 Days Challenge — Day 2: Longest Common Prefix Day 2 of my #LeetCode50DaysChallenge ✅ Today’s problem was all about string manipulation — Longest Common Prefix ✨ 🧩 Problem: Given an array of strings, find the longest common prefix among them. If there’s no common prefix, return an empty string "". 💡 Approach: I sorted the array alphabetically, then compared the first and last strings (since they’ll have the maximum difference). By comparing characters until they differ, I was able to build the common prefix using a StringBuilder. This approach is both elegant and efficient for the problem! ⏱️ Time Complexity: O(n × m) — where n is the number of strings and m is the length of the shortest string. 📊 Example: Input: ["flower", "flow", "flight"] Output: "fl" This problem really highlights how a simple sorting trick can simplify logic significantly. Feeling more confident with string problems — consistency is the key! 🔥 #LeetCode #CodingChallenge #Day2 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode
Solved Longest Common Prefix problem in LeetCode Challenge
More Relevant Posts
-
💻 LeetCode 50 Days Challenge — Day 3: Remove Duplicates from Sorted Array Day 3 of my #LeetCode50DaysChallenge ✅ Today’s problem was about array manipulation — Remove Duplicates from Sorted Array ✨ 🧩 Problem: Given an integer array nums sorted in non-decreasing order, remove duplicates in-place such that each unique element appears only once. The relative order of the elements should remain the same. 💡 Approach: This problem is a classic two-pointer approach! One pointer i keeps track of the last unique element’s position. The other pointer j iterates through the array. Whenever a new element is found (nums[i] != nums[j]), we move it forward by incrementing i and assigning nums[i] = nums[j]. In the end, i + 1 gives the count of unique elements. A simple yet elegant technique to modify arrays in-place! ⏱️ Time Complexity: O(n) 📊 Example: Input: [0,0,1,1,1,2,2,3,3,4] Output: [0,1,2,3,4] Consistency is the secret ingredient to progress! 🌱 Each problem solved adds another brick to the wall of mastery 💪 #LeetCode #CodingChallenge #Day3 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
💻 LeetCode 50 Days Challenge — Day 5: Search Insert Position Day 5 of my #LeetCode50DaysChallenge ✅ Today’s problem was about finding the correct insertion index in a sorted array efficiently — Search Insert Position ✨ 🧩 Problem: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. The goal is to achieve a runtime complexity of O(log n). 💡 Approach: I implemented a binary search approach. The idea is to use two pointers (left and right) to narrow down the range. If the middle element is less than the target, move the left pointer up. Otherwise, move the right pointer down. Once the pointers converge, the left index represents the correct insertion position. This method ensures an efficient and clean solution, making use of binary search logic rather than linear iteration. ⏱️ Time Complexity: O(log n) 📊 Example: Input: nums = [1, 3, 5, 6], target = 2 Output: 1 “Small steps every day lead to big progress — keep showing up!” 💪 #LeetCode #CodingChallenge #Day5 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 391 of #500DaysOfCode Today, I solved LeetCode Problem 2011: Final Value of Variable After Performing Operations 🧮 📝 Problem Summary: You start with a variable X = 0 and a list of operations such as "++X", "X++", "--X", or "X--". Each "++" increases the value by 1, and each "--" decreases it by 1. The goal is to return the final value of X after performing all operations. 💡 Approach: Initialize X = 0. Loop through the list of operations. If the operation contains "++", increment X. Otherwise, decrement X. Return the final result. ✅ Example: Input: ["--X","X++","X++"] Output: 1 A simple yet elegant problem that helps sharpen conditional logic and string manipulation fundamentals. ⚡ #LeetCode #CodingChallenge #Java #100DaysOfCode #500DaysOfCode #ProblemSolving #LearnEveryday
To view or add a comment, sign in
-
-
🚀 Just solved LeetCode 30 – Substring With Concatenation of All Words. 🧩 What was the problem? Given a string and a list of words (all same length), you have to find every starting index where all the words appear together as a continuous substring, in any order. Basically a sliding-window puzzle with fixed word sizes and frequency matching. 🛠️ What was my approach? • Counted all words using a map • Used multiple sliding windows based on word length • Expanded the window word by word • If a word appeared more than needed, I moved the left pointer forward • Added the index when the window matched every word exactly The key was treating the string in equal chunks, not character by character. 📚 What I learned Working with strings becomes much easier when you break the problem into predictable pieces. Managing window boundaries is the real challenge here, and walking through tiny examples helps more than you think. #LeetCode #Java #DSA #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 63 of My LeetCode journey 🚀 Problem : Custom Sort String Today’s problem was interesting because instead of sorting using normal alphabetical order, we sort the given string based on a custom priority order. 🧠 Problem Understanding Given: order → defines priority of characters. str → the input string which needs to be rearranged. Goal: ✅ Arrange characters of str based on the sequence defined in order. ✅ Characters not present in order should appear at the end (any order). 💡 Approach (Simple & Efficient) Count frequency of each character in str. First append characters following the order. Then append remaining characters. Time Complexity: O(n) Space Complexity: O(1) (fixed array size for 26 letters) ✨ Learnings Sometimes the problem isn't about sorting, but about following a custom priority. Frequency counting is extremely powerful for character-based problems. StringBuilder → best way to build strings efficiently in Java. #leetcode #coding #dsa #java #100DaysOfCode #day63 #learningEveryday #problemSolving
To view or add a comment, sign in
-
-
🌟 Day 56 of My #LeetCode Challenge – Word Pattern (#290) Today’s problem focused on validating if a string follows a specific character pattern — a great exercise in understanding mapping relationships and string manipulation. The task was to ensure that: 1️⃣ Each character in the pattern maps to exactly one word in the string. 2️⃣ No two characters map to the same word — maintaining a perfect one-to-one (bijection) relationship. To solve this, I used a HashMap<Character, String> to store the mappings between each character and its corresponding word. If a character already exists, I checked whether it points to the same word. If not, I returned false immediately. I also used containsValue() to make sure no word was reused for a different character. This problem reinforced the importance of careful logic flow, map lookups, and validation in both directions to avoid conflicts in mapping. 💡 Key learning: Building bijections with HashMaps is a common pattern in many string and mapping problems, and mastering it really improves problem-solving skills. #LeetCode #Day56 #Java #CodingChallenge #ProblemSolving #100DaysOfCode #DataStructures #HashMap #LearningEveryday
To view or add a comment, sign in
-
-
💻 Day 48 of #LeetCode100DaysChallenge Solved LeetCode 151: Reverse Words in a String — a simple yet elegant string manipulation problem that tests precision and edge-case handling. 🧩 Problem: Given a string s, reverse the order of words while removing extra spaces. Each word is a sequence of non-space characters, and the result should contain exactly one space between words with no leading or trailing spaces. 💡 Approach Used — String Splitting & Reversal: 1️⃣ Trim leading and trailing spaces. 2️⃣ Split the string by spaces to get individual words. 3️⃣ Filter out empty strings caused by multiple spaces. 4️⃣ Reverse the list of words and join them with a single space. ⚙️ Complexity: Time: O(N) Space: O(N) ✨ Key Takeaways: ✅ Strengthened understanding of string manipulation and trimming. ✅ Learned to handle irregular spacing efficiently. ✅ Practiced building clean, readable string-processing logic. #LeetCode #100DaysOfCode #Java #StringManipulation #ProblemSolving #DSA #CodingJourney #WomenInTech
To view or add a comment, sign in
-
-
✨ Day 57 of 100: Rotate List ✨ Today’s challenge was LeetCode 61 – Rotate List 🔄 Problem: Given the head of a linked list, rotate the list to the right by k places. Example: Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Key Idea: To rotate the list: 1️⃣ Find the length of the linked list. 2️⃣ Connect the tail to the head to make it circular. 3️⃣ Calculate the new head position as length - (k % length) steps from the start. 4️⃣ Break the circle at the right point to get the rotated list. 💡 Key Takeaways: Strengthened understanding of linked list traversal and manipulation. Learned to handle edge cases like k larger than the list length efficiently using modulo. Practiced converting a circular list back into a normal one at the right node. 💬 Every day of this challenge reinforces how small logical tweaks — like using % to handle rotations — can make code elegant and efficient. #100DaysOfCode #LeetCode #Java #LinkedList #CodingChallenge #DSA #ProblemSolving #WomenWhoCode
To view or add a comment, sign in
-
-
🚀 Day 118 of 120 – #DSAwithJava Challenge Hey LinkedIn fam! 👋 Today’s problem was all about Linked Lists and HashSets — a combination that makes node deletions efficient and elegant ⚡ ✅ Problem: Delete Nodes From Linked List Present in Array (LeetCode #3217 – Medium) 🎯 Objective: Given an array nums and the head of a linked list, remove all nodes whose values exist in nums. 🧠 Key Insight: Store all elements of nums in a HashSet for O(1) lookups. Use a dummy node before the head to handle edge deletions easily. Traverse the list, and if a node’s value exists in the set, skip it by adjusting pointers. The result is a clean linked list containing only valid nodes. 💡 Example: Input → nums = [1,2,3], head = [1,2,3,4,5] Output → [4,5] 🕒 Time Complexity: O(n + m) 📦 Space Complexity: O(m) 🔥 Takeaway: When dealing with linked lists, HashSets simplify membership checks, and dummy nodes simplify pointer logic. A small design tweak can lead to clean and optimal solutions! 💪 #120DaysOfCode #DSAwithJava #LeetCode #Java #LinkedList #HashSet #ProblemSolving #CodingChallenge #TechJourney #Consistency #LearnByDoing
To view or add a comment, sign in
-
-
🚀 Day 43 of #100DaysOfLeetCode Today's problem: LeetCode #160 – Intersection of Two Linked Lists 💡 Concept: Find the node where two singly linked lists intersect. Used the Two Pointer Approach — a smart and efficient way to solve this without using extra memory. 🧠 Logic: Move both pointers through the lists. When one pointer reaches the end, switch it to the other list’s head. They’ll either meet at the intersection node or end up as null together. ✅ Complexity: Time – O(n + m) Space – O(1) 💬 Takeaway: Sometimes, the best solutions come from balancing the path — literally! Understanding how pointers sync up teaches a lot about memory references and linked list behavior. #LeetCode #CodingChallenge #Java #DataStructures #TwoPointerTechnique #ProblemSolving #LinkedLists
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