🔥 Day 82 of #100DaysOfCode Today’s problem: LeetCode: Permutation in String 🧠 📌 Problem Summary Given two strings s1 and s2, return true if s2 contains a permutation of s1. Example: s1 = "ab" s2 = "eidbaooo" Output → true (because "ba" exists in s2) 🧠 Approach: Sliding Window + Frequency Count (Optimized) Key Idea: If a permutation exists, then some substring of s2 must: Have the same length as s1 Have the same character frequency ⚙️ Steps: If s1.length() > s2.length() → return false Maintain: int[26] s1Count int[26] s2Count Fill both arrays for first window Slide window across s2 Compare frequency arrays To optimize comparison: Track a matches counter When all 26 characters match → permutation found ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) (fixed 26 letters) 💡 What I Learned Sliding window problems become easier when you convert strings into frequency arrays. Tracking “matches” instead of comparing arrays every time improves performance. This is a classic interview problem for FAANG-level companies. Consistency is compounding. Sliding window mastery getting sharper every day. 🔥 On to Day 83 🚀 #100DaysOfCode #LeetCode #SlidingWindow #Java #DSA #CodingJourney #InterviewPrep
Permutation in String LeetCode Solution
More Relevant Posts
-
🔥 Day 83 of #100DaysOfCode Today’s problem: LeetCode: Minimum Size Subarray Sum 🎯 📌 Problem Summary Given: An integer target An array nums Return the minimum length of a contiguous subarray whose sum is greater than or equal to target. If no such subarray exists → return 0. Example: target = 7 nums = [2,3,1,2,4,3] Output → 2 (because [4,3] = 7) 🧠 Approach: Sliding Window (Two Pointers) This is a classic variable-size sliding window problem. ⚙️ Strategy: Use two pointers: l (left) and r (right) Expand window by moving r Keep adding to total When total >= target: Update result Shrink window from left Subtract from total 🔁 Core Logic: for each r: total += nums[r] while total >= target: update min length total -= nums[l] l++ ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 💡 What I Learned Sliding window is extremely powerful for subarray problems. The key is knowing when to expand and when to shrink. Many medium/hard problems are just variations of this pattern. Today’s runtime: ⚡ 1ms (99% faster) Consistency builds mastery. On to Day 84 🚀 #100DaysOfCode #LeetCode #SlidingWindow #Java #DSA #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 40 of #100DaysOfCode Solved 378. Kth Smallest Element in a Sorted Matrix on LeetCode 🔍📊 🧠 Key Insight: Each row and column in the matrix is sorted, so instead of flattening and sorting the matrix, we can apply Binary Search on the value range. ⚙️ Approach: 1️⃣ Define the search range: 🔹left = smallest element (matrix[0][0]) 🔹right = largest element (matrix[n-1][n-1]) 2️⃣ Perform binary search on values: 🔹Pick mid 🔹Count how many elements in the matrix are ≤ mid 3️⃣ If count ≥ k → move left side (potential answer found) 4️⃣ Else → move right side This efficiently narrows down the kth smallest element. ⏱️ Time Complexity: O(n log(max-min)) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Matrix #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 31 of #100DaysOfCode Solved 240. Search a 2D Matrix II on LeetCode 🔍📊 🧠 Key insight: In this matrix, each row is sorted left → right and each column is sorted top → bottom. Starting from the top-right corner lets us eliminate an entire row or column at each step. ⚙️ Approach: 🔹Start at top-right element 🔹If value equals target → return true 🔹If value is smaller → move down (increase row) 🔹If value is larger → move left (decrease column) 🔹Continue until the target is found or boundaries are crossed ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Matrix #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟑𝟔 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on converting a sorted array into a height-balanced Binary Search Tree (BST). 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Convert Sorted Array to Binary Search Tree 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Since the array is sorted, the middle element naturally becomes the root • Elements on the left side form the left subtree • Elements on the right side form the right subtree • Recursively applied the same logic to both halves This ensures the tree remains height-balanced. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Sorted arrays are perfect for constructing balanced BSTs • Choosing the middle element maintains balance • Divide-and-conquer simplifies tree construction • Recursion helps build hierarchical structures naturally 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(log n) (recursion stack) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the structure of the input already hints at the best tree structure. 36 days consistent 🚀 On to Day 37. #DSA #Arrays #BinarySearchTree #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
LeetCode 1593 – Split a String Into the Max Number of Unique Substrings An ideal problem to understand Backtracking. The task is simple but tricky: Split a string into substrings such that all substrings are unique, and maximize the number of splits. 🚀 Approach (Backtracking) 1. Start from index i and try every possible substring s[i...j]. 2. If the substring is not already used (checked using a HashSet), we: add it to the set recursively explore the remaining string starting from j + 1 3. Each recursive call increases the current split count. 4. When we reach the end of the string (i >= length), we update the maximum number of unique splits. 5. After recursion, we remove the substring from the set (backtrack) so other possibilities can be explored. # Core Backtracking Pattern Choose → Explore → Undo Choose: Add substring to the set Explore: Recurse for the remaining string Undo: Remove substring to try other partitions Backtracking explores all possible partitions, but the HashSet ensures no substring repeats, giving the maximum valid split. Perfect example of how recursion + state tracking can systematically search the solution space. #LeetCode #Backtracking #Java #DSA #CodingInterview #Recursion
To view or add a comment, sign in
-
-
Solved: Binary Tree Maximum Path Sum (LeetCode – Hard) I recently solved the Maximum Path Sum problem in a binary tree, a classic interview question that strengthens recursion and tree traversal concepts. ->Used postorder traversal (process left → right → node). ->Ignored negative subtree sums to avoid reducing the total. ->Maintained a global variable to track the maximum path found so far. ->Returned only the best single-branch path to the parent node. Key Takeaways: 1. Clear understanding of recursion flow is essential. 2. Careful handling of negative values improves correctness. Time Complexity: O(N) Space Complexity: O(H) #LeetCode #Java #BinaryTree #DataStructures #CodingInterview
To view or add a comment, sign in
-
-
Understanding Tree Height Using Level Order Traversal (BFS) Today I revised an important Binary Tree concept — calculating the height of a tree using Queue (Level Order Traversal). Initially, I used the null marker approach to track levels: Add root to queue Add null to mark end of level Increment height when null is encountered Add another null if queue is not empty While this works, it adds extra conditional checks and complexity. 💡 Then I switched to a cleaner approach using the queue’s size(): ✔ Count number of nodes at current level ✔ Process all nodes of that level ✔ Increment height after each level This approach is: More readable More efficient Interview-friendly Preferred in product-based company interviews 🔑 Key Learning: Sometimes a solution works, but refining it makes it cleaner and more optimal. Writing better logic > just writing working code. #DSA #BinaryTree #LevelOrderTraversal #Java #CodingJourney
To view or add a comment, sign in
-
-
Day 20/30 – Add Two Numbers 🚀 Problem: Add two numbers represented as linked lists. Key Concepts: • Digit-by-digit addition • Carry handling • Dummy node usage Approach: 1️⃣ Traverse both lists 2️⃣ Add values + carry 3️⃣ Create new node (sum % 10) 4️⃣ Update carry (sum / 10) Time Complexity: O(max(n, m)) Space Complexity: O(max(n, m)) Classic simulation problem. #30DaysOfCode #Java #DSA #LinkedList #InterviewPrep
To view or add a comment, sign in
-
-
🚀 #50DaysDSAChallenge | Day 38/50 🚀 🧩 Problem: Merge Strings Alternately 📍 Platform: LeetCode (Easy) 💻 Solution: https://lnkd.in/gGae9M96 💭 Problem Overview: You are given two strings word1 and word2. Merge them by adding letters in alternating order, starting with word1. If one string is longer, append the remaining characters at the end. 🛠️ Approach Used (Two Pointers + StringBuilder): Find the minimum length of both strings Append characters alternately up to the minimum length Append the remaining substring of the longer string ⚡ Why this approach? Simple and readable Avoids unnecessary checks inside loops Efficient string concatenation using StringBuilder ⏱️ Complexity Analysis: Time Complexity: O(n + m) Space Complexity: O(n + m) 🔑 Key Learning: String problems often benefit from StringBuilder Handling unequal lengths cleanly avoids edge-case bugs Converting strings to character arrays simplifies indexing Clear separation of logic improves readability ✅ Status: Solved (Optimal) #50DaysDSAChallenge #Day38 #LeetCode #MergeStringsAlternately #Strings #TwoPointers #Java #DSA #ProblemSolving
To view or add a comment, sign in
-
🚀 Day 45 / 100 | Longest Substring Without Repeating Characters Intuition: The problem is to find the length of the longest substring without repeating characters. If a repeated character appears, the substring must be adjusted to remove duplicates. Using a sliding window approach, it maintain a window of unique characters while scanning the string. Approach: O(n) -Use a HashSet to store characters currently in the substring window. -Maintain two pointers: left and right . -Iterate through the string using the right pointer. -If the character already exists in the set, remove characters from the left until the duplicate is removed. -Add the current character to the set. -Update the maximum length of the substring using the window size. -Repeat this process until the end of the string. Complexity: Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
To view or add a comment, sign in
-
Explore related topics
- Approaches to Array Problem Solving for Coding Interviews
- Java Coding Interview Best Practices
- Tips for Coding Interview Preparation
- Common Coding Interview Mistakes to Avoid
- Why Use Coding Platforms Like LeetCode for Job Prep
- How to Improve Array Iteration Performance in Code
- Amazon SDE1 Coding Interview Preparation for Freshers
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