Day 22 of #100DaysOfCode Today I solved the “Permutation in String” problem on LeetCode — a tricky one that really sharpens your understanding of sliding window and frequency mapping in strings. 🧩 Problem in short: Given two strings s1 and s2, the task is to check if any permutation of s1 exists as a substring in s2. At first glance, it feels like a brute-force problem — generate all permutations of s1 and check each in s2. But that’s computational suicide for longer strings. So the challenge was to find a smarter, optimized approach. ⚙️ Intuitive Approach: Instead of generating permutations, I focused on character frequencies. Maintain two frequency arrays — one for s1 and one for the current window of s2. Slide the window across s2, adding and removing characters as you move. Whenever both frequency arrays match, it means that substring of s2 is a permutation of s1. This approach reduces the complexity drastically and relies on pattern recognition through frequency matching, not brute force. Every day in this challenge is a reminder that optimization isn’t about doing less work — it’s about doing the right work. #LeetCode #C++ #ProblemSolving #DSA #CodingJourney #100DaysOfCode
Solved "Permutation in String" problem on LeetCode with frequency mapping
More Relevant Posts
-
🔗 Day 63 of #100DaysOfCode 🔗 🔹 Problem: Valid Parentheses – LeetCode ✨ Approach: Implemented a stack-based validation to ensure every opening bracket has a matching closing one in correct order. Each character is checked systematically — pushing opens and popping closes — making it both clean and efficient! ⚡ 📊 Complexity Analysis: Time Complexity: O(n) — single traversal of the string Space Complexity: O(n) — stack usage for unmatched brackets ✅ Runtime: 2 ms (Beats 97.47%) ✅ Memory: 41.96 MB 🔑 Key Insight: Stacks are the unsung heroes of structured logic — from parentheses validation to syntax parsing, they keep the balance just right. 🧠 #LeetCode #100DaysOfCode #ProblemSolving #DSA #Stack #AlgorithmDesign #CodeJourney #ProgrammingChallenge #LogicBuilding #Efficiency #CodingDaily
To view or add a comment, sign in
-
-
🔹 Day 69 of #100DaysOfLeetCodeChallenge 🔹 🚀 Problem: Subsets II 🔑 Topic: Backtracking 🧠 Approach: The task is to find all unique subsets of an array that may contain duplicates. Here’s the step-by-step logic 👇 Sort the array first to bring duplicates together. Use backtracking to explore every possible subset. Skip duplicates by checking if the current element equals the previous one at the same recursion depth. Add every subset (including empty) to the result list. ⏳ Time Complexity: O(2^n) 💾 Space Complexity: O(n) (recursion stack + temp list) 📌 Example: Input: nums = [1,2,2] Output: [[], [1], [1,2], [1,2,2], [2], [2,2]] ✅ 🎯 Takeaway: Sorting and skipping duplicates isn’t just for combinations — it’s the secret weapon for clean subset generation too! ⚡ #LeetCode #DSA #Backtracking #Subsets #ProblemSolving #CodingChallenge #100DaysOfLeetCodeChallenge 🚀
To view or add a comment, sign in
-
-
🔢 “Today was all about turning strings into numbers — one recursive call at a time.” 🚀 Day 54/150 – DSA Challenge ✅ Problem solved: String to Integer (atoi) – LeetCode 8 (Recursion Approach) Converting a string to an integer sounds simple… until you start handling edge cases. Steps I followed: 1. Skip leading spaces. 2. Check for sign (+ / -). 3. Parse digits recursively. 4. Handle overflow by bounding between INT_MIN and INT_MAX. The recursion gave it a clean logical flow — digit by digit, call by call. ⏳ Time: O(n) 📦 Space: O(n) (recursion stack) 💡 “Recursion isn’t just looping — it’s trusting the next call to finish what you started.” 🔥 From text to integers, one call at a time. #Day54 #DSA #150DaysChallenge #LeetCode #StriverSheet #Recursion #ProblemSolving #CodingJourney #Atoi
To view or add a comment, sign in
-
-
Day 105 of 365 of Solving LeetCode Problems! ✅ Generate All Binary Strings: Given an integer n, generate all binary strings of length n representing bits. Return the strings in ascending order. Key Observations: 1) This is a classic recursion and backtracking problem — each position can either be '0' or '1'. 2) You explore both possibilities recursively until the string reaches length n. 3) The order of recursive calls ('0' first, then '1') ensures the results are in lexicographical (ascending) order. 4) Time complexity is O(2^n) since there are 2^n possible binary strings. #Recursion #Backtracking #LeetCode #DSA
To view or add a comment, sign in
-
-
🚀 Day 125 of #LeetCode Challenge! Problem: Binary Tree Preorder Traversal 💡 My Approach: The goal is to perform a preorder traversal of a binary tree — visiting nodes in the order: Root → Left → Right Here’s the step-by-step logic: Start from the root node. Visit (record) the root value first. Recursively traverse the left subtree, then the right subtree. Store values in a vector as you go. ✨ Example Input: [1, null, 2, 3] Output: [1, 2, 3] 🧠 Key Idea Preorder traversal is useful when you need to copy or serialize a tree — it captures the structure starting from the root. ⏱ Complexity TypeValueTimeO(N) — each node visited onceSpaceO(H) — recursion stack (H = height of tree) 📎 GitHub Link: https://lnkd.in/gZ6GeXzm #LeetCode #BinaryTree #PreorderTraversal #Recursion #DSA #C++ #ProblemSolving #CodingChallenge #Day125
To view or add a comment, sign in
-
-
🚀 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 𝟑𝟐𝟐𝟖-𝐌𝐚𝐱𝐢𝐦𝐮𝐦 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬 𝐭𝐨 𝐌𝐨𝐯𝐞 𝐎𝐧𝐞𝐬 𝐭𝐨 𝐭𝐡𝐞 𝐄𝐧𝐝 🧩 Problem Statement: You’re given a binary string s. You can repeatedly choose an index i where s[i] == '1' and s[i + 1] == '0', and move that '1' to the right until it reaches the end or another '1'. You need to return the maximum number of operations that can be performed. Example: Input: s = "1001101" Output: 4 🧠 Approach: The idea is to notice that every '1' before a block of '0' can move once for that block. So, as we traverse the string :- • Keep counting '1's as they appear. • When we encounter a block of '0's, add the total number of '1's seen so far to our answer. • This way, each zero block contributes operations equal to the number of ones before it. Time Complexity: O(n) Space Complexity: O(1) #LeetCode #ProblemSolving #DSA #CodingJourney #LogicBuilding #DailyChallenge #KeepLearning #100DaysOfCode
To view or add a comment, sign in
-
-
Day 25 of #100DaysOfCode Today I tackled the “Find All Anagrams in a String” problem on LeetCode — a great challenge that builds directly on the concept of sliding window and frequency comparison. 🧩 Problem Summary: Given two strings s and p, the task is to find all start indices in s where an anagram of p appears as a substring. At first, it might look similar to the “Permutation in String” problem — and that’s exactly the connection! The difference here is that instead of just checking if such a substring exists, we need to find all of them. ⚙️ Intuitive Approach: 1. Count character frequencies for p. 2. Use a sliding window over s of size equal to p.length(). 3. Maintain a frequency map for the current window and compare it with p’s frequency map. 4. Every time the maps match → we’ve found an anagram’s starting index. This problem reinforced how powerful the sliding window + frequency array technique can be when dealing with substring-based string problems. Each problem I solve reminds me that mastering patterns is far more valuable than memorizing solutions. #LeetCode #C++ #DSA #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 93 of My #100DaysOfCode Challenge! Today, I explored one of the most fundamental and elegant problems in Binary Trees — the Diameter of a Binary Tree 🌳 using C++. The goal of this problem is to find the longest path between any two nodes in a binary tree. This path may or may not pass through the root. To solve it efficiently, I used recursion and the concept of tree height. 💡 What I Learned: 🔹 Height Function: The height of a node is 1 + max(height(left), height(right)). I built a recursive function height(TreeNode* root) that returns the height of any subtree. 🔹 Diameter Function: For each node, I calculated three possible diameters: Diameter of the left subtree Diameter of the right subtree Diameter passing through the current node (left height + right height) The maximum of these three gives the current diameter. 🔹 Key Insight: This approach uses recursion twice (once for height and once for diameter), which makes it O(N²) — a good starting point for understanding the logic before optimizing it to O(N) with a single traversal. 🧠 Concepts Strengthened: Recursive Tree Traversal Depth & Height calculation Problem decomposition using subproblems Understanding tree structure visualization 🌱 Slowly mastering trees — one recursive function at a time! Next step: Optimize the diameter function for O(N) time complexity using a single DFS call. #Day93 #100DaysOfCode #CPlusPlus #DSA #BinaryTree #Recursion #ProblemSolving #CodingJourney #LeetCode
To view or add a comment, sign in
-
-
🧩 LeetCode Challenge – Day 72 ✅ Dived into a string decoding problem today — a perfect blend of stacks, recursion, and pattern tracking. 🔗 LeetCode 394 – Decode String This challenge revolves around decoding expressions like 3[a2[c]], requiring careful handling of nested patterns and character reconstruction. It’s a great exercise in managing multiple layers of logic — keeping track of counts, substrings, and the overall decoded output step by step. 💡 Key Takeaways: • Stack-based parsing builds precision in handling nested structures. • Breaking problems into layers simplifies even the most complex logic. • Attention to detail is everything — one misplaced index can change the entire output. #Day72 #LeetCodeChallenge #100DaysOfCode #DSA #CodingJourney #ProblemSolving #Stack #Strings #Parsing #Recursion
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
Excellent breakdown, Harsh! 🔥 The way you explained the frequency array approach shows real depth in problem-solving. Love that line “optimization isn’t about doing less work, it’s about doing the right work.” 💯 Keep it up!