Day 87: Reconstructing Strings from LCP 🔍 Problem 2573: Find the String with LCP Today’s challenge was a fascinating "reverse engineering" problem. Given a Longest Common Prefix (LCP) matrix, the goal was to reconstruct the original string—or determine if such a string even exists. The Strategy: • Greedy Character Assignment: I used the LCP matrix to group indices that must share the same character. If the LCP between two indices is greater than 0, they belong to the same character group. • Alphabet Constraint: Since we only have 26 lowercase letters, I checked if the number of required unique groups exceeded our available alphabet. • Verification via DP: Generating a string isn't enough; you have to prove it matches the original matrix. I built a 2D DP table to calculate the actual LCP of my reconstructed string and compared it against the input grid. • The "LCP Property": Used the relation dp[i][j] = 1 + dp[i+1][j+1] (if characters match) to verify the matrix in O(N²). It’s one thing to build a solution that looks right, but verifying it against the constraints is where the real logic happens. Another day of sharp problem-solving in the books. 🚀 #LeetCode #Java #DynamicProgramming #StringAlgorithms #DailyCode
Reconstructing Strings from LCP Matrix with LeetCode Challenge
More Relevant Posts
-
Day 3 / 75 – DSA Challenge Solved “Longest Substring Without Repeating Characters” using the Sliding Window technique. Optimized the solution to achieve: • Time Complexity: O(n) • Space Complexity: O(1) (fixed-size frequency array) Focused on improving window management logic and reducing unnecessary computations. The solution was accepted with strong runtime and memory performance. Consistent progress, one problem at a time. #75DaysOfDSA #DataStructures #Algorithms #Java #ProblemSolving #LeetCode
To view or add a comment, sign in
-
-
Adding strict compile time evaluation 'consteval' to C# compiler 'Roslyn'. The post is great if you are unfamiliar with Roslyn , as I will go deeply in its internals explaining the different layers of it while adding the new C# compile time evaluator , so I really encourage you to do so or you can just jump right into the code Here is my Roslyn fork: https://lnkd.in/dGzexHHf And my post about the feature (Hope you enjoy it 😃): https://lnkd.in/d-dFHXP
To view or add a comment, sign in
-
Day 48 of Daily DSA 🚀 Solved LeetCode 48: Rotate Image ✅ Problem: Given an n x n matrix, rotate the image by 90° clockwise — in-place (without using extra space). Approach: Used a two-step transformation: Transpose the matrix Reverse each row Steps: Traverse upper triangle and swap → matrix[i][j] ↔ matrix[j][i] For each row: Use two pointers (left, right) Swap elements to reverse the row Matrix gets rotated in-place ⏱ Complexity: • Time: O(n²) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.56 MB In-place transformations are powerful — no extra space, just smart manipulation 💡 #DSA #LeetCode #Java #Matrix #Arrays #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 63/75 — Rotate Array Today’s problem was about rotating an array to the right by k steps. Approach: • Use reversal algorithm for optimal in-place rotation • Reverse entire array • Reverse first k elements • Reverse remaining elements Key logic: k = k % n; reverse(nums, 0, n - 1); reverse(nums, 0, k - 1); reverse(nums, k, n - 1); Time Complexity: O(n) Space Complexity: O(1) A classic array problem that reinforces in-place manipulation techniques. 63/75 🚀 #Day63 #DSA #Arrays #InPlace #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 49/75 — Rearrange Array Elements by Sign Today’s problem focused on rearranging an array such that positive and negative numbers alternate, while preserving their original order. Approach: • Use two pointers (even index for positive, odd index for negative) • Traverse the array once • Place elements in correct positions directly Key logic: if (num >= 0) { result[posIdx] = num; posIdx += 2; } else { result[negIdx] = num; negIdx += 2; } Time Complexity: O(n) Space Complexity: O(n) A clean problem that reinforces index placement and pattern-based traversal. 49/75 🚀 #Day49 #DSA #Arrays #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 56/100 | #100DaysOfDSA 🧠⚡ Today’s problem: String Compression A clean in-place array manipulation problem. Core idea: Compress consecutive repeating characters and store the result in the same array. Approach: • Traverse the array using a pointer • Count consecutive occurrences of each character • Write the character to the array • If count > 1 → write its digits one by one • Move forward and repeat Key insight: We don’t need extra space — just carefully manage read & write pointers. Time Complexity: O(n) Space Complexity: O(1) Big takeaway: In-place algorithms require precise pointer control but give optimal space efficiency. Mastering these improves real-world memory optimization skills. 🔥 Day 56 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Strings #TwoPointers #InPlace #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 12 of #100DaysOfCode — Sliding Window Today, I worked on the problem “Max Consecutive Ones III” LeetCode. Problem Summary Given a binary array, the goal is to find the maximum number of consecutive 1s if you can flip at most k zeros. Approach At first glance, this problem looks like a brute-force or restart-based problem, but the optimal solution lies in the Sliding Window technique. The key idea is to maintain a window [i, j] such that: The number of zeros in the window does not exceed k Expand the window by moving j Shrink the window by moving i whenever the constraint is violated Instead of restarting the window when the condition breaks, we dynamically adjust it. Key Logic Traverse the array using pointer j Count the number of zeros in the current window If zeros exceed k, move pointer i forward until the window becomes valid again At every step, update the maximum window size Why This Works This approach ensures: Each element is processed at most twice Time Complexity: O(n) Space Complexity: O(1) The most important learning here is understanding how to dynamically adjust the window instead of resetting it, which is a common mistake while applying sliding window techniques. In sliding window problems, always focus on expanding and shrinking the window efficiently rather than restarting the computation. #100DaysOfCode #DSA #SlidingWindow #LeetCode #Java #ProblemSolving #CodingJourney #DataStructures #Algorithms
To view or add a comment, sign in
-
-
Day 45 of Daily DSA 🚀 Solved LeetCode 867: Transpose Matrix ✅ Problem: Given a 2D matrix, return its transpose (rows become columns and columns become rows). Approach: Created a new matrix and swapped indices while traversing. Steps: Get number of rows and columns Create a new matrix of size cols x rows Traverse original matrix Assign: trans[j][i] = matrix[i][j] Return the new matrix ⏱ Complexity: • Time: O(n × m) • Space: O(n × m) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 46.46 MB Simple transformations like transpose build strong fundamentals for matrix-based problems 💡 #DSA #LeetCode #Java #Arrays #Matrix #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 10/30 of #30DaysOfDSA 🔹 Problem: Two Sum II (Sorted Array) 💡 Approach: Initially, I thought of using a brute force approach by checking every pair of elements to find the target sum. While this works, it takes O(n²) time which is not efficient. Then I realized the array is already sorted, so I used the two-pointer technique. I placed one pointer at the beginning (i) and another at the end (j). If the sum of both elements equals the target, I return their indices. If the sum is less than the target, I move the left pointer forward. If the sum is greater, I move the right pointer backward. This way, I was able to solve the problem efficiently in a single pass. ⚡ What I Learned: • Optimizing from brute force to efficient solution • Smart use of two-pointer technique on sorted arrays • How sorting helps reduce complexity • Writing clean and optimal logic ⏱️ Complexity: O(n) time | O(1) space #DSA #Coding #Java #Consistency #LearningJourney
To view or add a comment, sign in
-
-
Day 46 #SDE prefix sum and sliding window techniques. Solved: • Binary Subarrays With Sum • Maximum Points You Can Obtain from Cards Key Learning: • “Binary Subarrays With Sum” → Two strong approaches: – Prefix sum + hashmap (counting subarrays efficiently) – At-most K sliding window → exactly(K) = atMost(K) - atMost(K-1) • “Maximum Points from Cards” → Classic optimization: – Instead of picking k elements, remove a window of size (n - k) – Convert problem into minimizing subarray sum #LeetCode #DSA #SlidingWindow #PrefixSum #Algorithms #Java #SoftwareEngineering
To view or add a comment, sign in
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