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
Solved "Find All Anagrams in a String" on LeetCode using sliding window and frequency comparison.
More Relevant Posts
-
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
To view or add a comment, sign in
-
-
🔗 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
-
-
#leetcode #10 #Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Example 1: Input: s = "aa", p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa", p = "a*" Output: true Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab", p = ".*" Output: true Explanation: ".*" means "zero or more (*) of any character (.)".
To view or add a comment, sign in
-
-
✅ Day 3 / 75 🌟 Today’s Progress 1️⃣ Top K Frequent Elements — LeetCode Medium Given an integer array nums and an integer k, return the k most frequent elements. 💡 Approach: Used a hash map to count the frequency of each number. Stored {frequency, element} pairs in a vector. Sorted the vector in descending order by frequency. Picked the first k elements — those are the most frequent. 2️⃣ Encode and Decode Strings — LeetCode Medium Problem: Design an algorithm to encode a list of strings into a single string, then decode it back to the original list. 💡 Approach: Added the length of each string before it, followed by a delimiter (#). While decoding, read the number before # to know how many characters belong to that string. #Day3 #Leetcode #NeetCode #DSA #ProblemSolving #CodingDaily
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 41 – LeetCode Practice Problem: Greatest Common Divisor of Strings (LeetCode #1071) 📌 Problem Statement: Given two strings str1 and str2, return the largest string x such that both strings can be formed by repeating x. If no such string exists, return an empty string. --- ✅ My Approach: 1. String Compatibility Check: Verified if str1 + str2 equals str2 + str1. If they don’t match, there is no common divisor string. 2. Math Logic – GCD of Lengths: Computed the greatest common divisor (GCD) of the lengths of both strings. 3. Final Result: The answer is the prefix of the string with length equal to the GCD value. --- 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) --- ⚡ Submission Results: ✅ Accepted ⏱ Runtime: 0 ms (Beats 100%) 🚀 💾 Memory: Beats 64% --- 💡 Reflection: Loved this problem — it's a perfect example of how math (GCD) + string logic can simplify things dramatically. Elegant, efficient, and satisfying to solve!
To view or add a comment, sign in
-
-
Learning By Solving Problems on Strings 💻 Problem I Solved ✔️LeetCode #443 – String Compression 🔍 Key Insights Gained 1️⃣ Learned to handle in-place string compression efficiently. 2️⃣Strengthened focus on writing optimized and readable code. 3️⃣ Realized the power of StringBuilder for cleaner debugging. 💡"First, solve the problem. Then, write the code."
To view or add a comment, sign in
-
-
Day 109 of 365 of Solving LeetCode Problems! Check if Digits Are Equal in String After Operations: You are given a numeric string s. In each operation, replace the string with a new one formed by taking the sum (mod 10) of every two consecutive digits until the string’s length becomes 2 or less. Finally, check if both digits in the resulting string are equal. Key Observations: 1) The core idea is to simulate the process — repeatedly compressing the string by summing adjacent digits mod 10. 2) Each iteration reduces the string size by one, so the process always converges. 3) Once the string length is ≤ 2, only equality of the two remaining digits matters. 4) Efficiently managing indices and resetting after each compression ensures correctness. #LeetCode #DSA
To view or add a comment, sign in
-
-
✅ Day 4/75 🌟 Today’s Progress 1️⃣ Longest Consecutive Sequence — LeetCode Medium Problem: Given an unsorted array of integers, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time. 🔹Approaches: 1️⃣ Sorting Approach (Brute Force) Sorted the array and count the consecutive elements. Time Complexity: O(N log N). 2️⃣ Optimal – HashSet Stored all the numbers in a HashSet. For each number, checked if it’s the start of a sequence (i.e., num - 1 not in set). Then counted how long the consecutive sequence continues. ✅ Time: O(N) | Space: O(N) 2️⃣ Product of Array Except Self — LeetCode Medium Problem: Given an array nums, return an array answer such that answer[i] equals the product of all elements except nums[i], without using division, in O(n) time. 🔹Approaches: 1️⃣ Brute Force: Multiplied all elements except self for each index. ⏱️ Time: O(N²) 2️⃣ Optimal – Prefix & Suffix Product First pass: calculated the prefix products (product of all elements to the left). Second pass: multiplied each element by the suffix product (product of all elements to the right). ✅ Time: O(N) | Space: O(1) (excluding output array) On to Day 5 tomorrow! ✨ #NeetCode #Blind75 #DSA #LeetCode #CodingChallenge #ProblemSolving
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
-
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