🚀 Day 3 – Strings, Patterns & Optimizations! Today I dove deep into string problems and realized how much optimization matters. 💡 Highlights from Day 3: Anagram Check: Learned to map characters using frequency arrays. Why c - 'a' works now makes total sense. String Compression: Counting frequencies vs handling consecutive duplicates efficiently. Realized StringBuilder is a life-saver for performance. KMP Algorithm: Pattern searching without restarting. LPS array is the secret sauce! Learned to find patterns in O(n + m) time. 🔥 Key Lesson: It’s not just about solving a problem—it’s about solving it efficiently. Knowing the theory behind the code makes all the difference. From brute-force thinking → optimized thinking, every day I’m leveling up my coding skills. #DSA #Java #100DaysOfCode #CodingJourney #ProblemSolving #LearningDaily
Optimizing String Problems with Frequency Arrays and StringBuilder
More Relevant Posts
-
Day 65/100 | #100DaysOfDSA 🧩🚀 Today’s problem: N-Queens A classic hard backtracking problem. Problem idea: Place N queens on an N×N chessboard such that no two queens attack each other. Key idea: Use backtracking with efficient conflict checking. Why? • We must explore all valid board configurations • Reject placements that cause conflicts • Continue building only valid states How it works: • Place one queen per row • Track columns, diagonals, anti-diagonals • Use bit masking for faster checks ⚡ • Try placing → recurse → backtrack Optimization: Using bitmasks drastically reduces time compared to naive checking. Time Complexity: Exponential (but optimized with pruning) Space Complexity: O(n) recursion depth Big takeaway: Efficient state tracking (cols + diagonals) turns a brute-force problem into a much faster solution. 🔥 One of the most important backtracking problems to master! Day 65 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #Backtracking #Recursion #BitManipulation #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day: 73/365 📌 LeetCode POTD: Minimum Absolute Difference in Sliding Submatrix Medium Key takeaways/Learnings from this problem: 1. This problem shows how sliding window ideas extend to 2D, not just arrays, but it gets trickier to manage. 2. Maintaining a sorted structure (like multiset) helps in quickly finding min absolute differences in each window. 3. Big learning: brute force over every submatrix is too slow, so optimize by reusing previous window computations. 4. Overall, it’s a solid mix of 2D traversal + smart data structures to keep things efficient. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
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
-
-
📌 LeetCode Daily Challenge — Day 27 Problem: 3786. Cyclic Shifts of a Matrix Topic: Array, Matrix, Math, Simulation 📌 Quick Problem Sense: You're given an m × n matrix and an integer k. Every step: even-indexed rows shift left, odd-indexed rows shift right — repeated k times. Return true if the matrix after k steps is identical to the original. The catch? k can be huge — so simulating step by step is a dead end. 🚫 🧠 Approach (Simple Thinking): 🔹 A row of length n always returns to original after n shifts — but it might return sooner! 🔹 The minimum cyclic period of a row = the smallest divisor d of n such that the row is just a block of size d repeated 🔹 Check it: row[j] == row[j % d] for all j — if true, d is the period! 🔹 Direction (left vs right) doesn't affect the period — both complete their cycle in the same number of steps 🔹 The matrix restores to original iff k % period == 0 for every row 🔹 Get all divisors of n once in O(√n), check each row against them — no simulation needed! 🎯 ⏱️ Time Complexity: Divisors of n → O(√n) Per row period check → O(d(n) × n) All rows → O(m × d(n) × n) Blazing fast — no k-step simulation ever needed! 📦 Space Complexity: Just a divisors list → O(√n) Zero extra matrix copies or simulation buffers! I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/gbqN_Y7a If you solved it using the KMP failure function to find the string period in O(n), I'd love to see that approach, drop it in the comments 💬 See you in the next problem 👋 #LeetCode #DSA #CodingChallenge #Java #ProblemSolving
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 55/75 — Shortest Subarray with Sum at Least K Today’s problem was a challenging one involving prefix sums and a monotonic deque. Approach: • Use prefix sum to convert subarray sum into difference of indices • Maintain a deque to keep indices in increasing order of prefix sums • Remove useless indices to keep it optimal Key logic: while (!dq.isEmpty() && ps[j] - ps[dq.peekFirst()] >= k) { minLen = Math.min(minLen, j - dq.pollFirst()); } while (!dq.isEmpty() && ps[j] <= ps[dq.peekLast()]) { dq.pollLast(); } dq.offerLast(j); Time Complexity: O(n) Space Complexity: O(n) A tough problem that builds strong intuition for prefix sums + deque optimization. 55/75 🚀 #Day55 #DSA #PrefixSum #Deque #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 51/100 | #100DaysOfDSA 🚀⚡ Today’s problem: Product of Array Except Self Given an array, return a new array where each element is the product of all elements except itself — without using division. Brute force? Too slow. Division? Not allowed. Optimized Approach: • Build prefix product array (left to right) • Then multiply with suffix product (right to left) • No extra space needed (excluding output) Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Breaking a problem into prefix + suffix patterns can unlock optimal solutions. Clean logic. Efficient approach. 💯 Day 51 done. 🔥 #100DaysOfCode #LeetCode #DSA #Algorithms #Arrays #PrefixSum #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Today I solved LeetCode 110 – Balanced Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, determine if it is height-balanced. A binary tree is balanced if: The height difference between the left and right subtree of every node is not more than 1. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Height calculation 🧠 Approach: Use DFS to calculate the height of each subtree. For every node: Get height of left subtree Get height of right subtree Check if the absolute difference is greater than 1: If yes → tree is not balanced Optimize by returning -1 early when imbalance is found to avoid unnecessary calculations. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Combining height calculation with validation in one traversal. Optimizing recursive solutions with early stopping. Understanding balanced vs unbalanced trees. Strengthening DFS and recursion skills. Improving step by step with tree problems Consistency is building confidence every day #LeetCode #DSA #BinaryTree #BalancedTree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
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 103: Simple & Clean 🎯 Problem 1848: Minimum Distance to the Target Element After some complex DP challenges, today was a straightforward exercise in linear search and distance calculation. The Strategy: • Linear Traversal: I iterated through the array to find every occurrence of the target element. • Absolute Minimization: For each match, I calculated the absolute difference between the current index and the start index, keeping track of the minimum value found. Sometimes a simple, O(N) solution is all you need. Day 103 down—maintaining the streak with clarity and consistency. 🚀 #LeetCode #Java #Algorithms #ProblemSolving #DailyCode
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