🚀 Day 63/100 – #LeetCodeChallenge 📌 Problem: Count Binary Substrings Today’s problem was all about spotting patterns in binary strings — and it turned out to be a fun exercise in run-length encoding logic! 🧠 🔍 Approach: Instead of storing counts separately, I used a simple linear scan to keep track of consecutive identical characters (strk) and the length of the previous run (prev). Whenever a run ends, we update prev and reset strk. Then, if the current streak is ≤ the previous one, it means we’ve found a valid substring — and we increment the result. 📈 ⚡ Result: ✅ Runtime: 10 ms – beats 87.50% 🚀 💾 Memory: 46.44 MB – beats 56.68% Feeling good about the balance between time and space efficiency here! Every day is a step closer to mastering problem-solving patterns. 💪 🔗 Takeaway: Sometimes the most elegant solutions come from simple observation — no need for complex data structures when a single pass and a few variables do the trick! #100DaysOfCode #LeetCode #Java #CodingChallenge #ProblemSolving #TechJourney #DevCommunity #StringManipulation #Algorithms #CodeNewbie #SoftwareEngineering #WomenInTech #CodingLife #InterviewPrep #DataStructures #DailyCoding
Count Binary Substrings with Run-Length Encoding Logic
More Relevant Posts
-
𝗗𝗮𝘆 𝟲𝟵/𝟭𝟬𝟬 — 𝗥𝗲𝗺𝗼𝘃𝗲 𝗞 𝗗𝗶𝗴𝗶𝘁𝘀 Day 69. One day from 70. This problem broke my brain. Then it clicked. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟰𝟬𝟮: Remove K Digits (Medium) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a number as a string and k, remove k digits to make the smallest possible number. Example: "1432219", k=3 → "1219" Which 3 digits do you remove? And in what order? 𝗧𝗵𝗲 𝗜𝗻𝘀𝗶𝗴𝗵𝘁: Greedy + Monotonic Stack. Remove a digit if the next digit is smaller. Keep the stack increasing. This builds the smallest number left-to-right. Example: "1432219" See '4' then '3'? Remove '4' See '3' then '2'? Remove '3' See '2' then '2'? Keep both Result: "12219" → remove trailing '9' → "1219" 𝗠𝘆 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: StringBuilder as stack. For each digit: While stack top > current digit AND k > 0: pop and decrement k Append current digit After loop, remove remaining k digits from the end. Strip leading zeros. Time: O(n), Space: O(n) 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: This combines greedy thinking with monotonic stack. Two patterns, one solution. Understanding when to be greedy and when to use a stack—that's the skill. 𝗖𝗼𝗱𝗲: https://lnkd.in/gv36YUfj 𝗗𝗮𝘆 𝟲𝟵/𝟭𝟬𝟬 ✅ 𝟲𝟵 𝗱𝗼𝘄𝗻. 𝟯𝟭 𝘁𝗼 𝗴𝗼. 𝗧𝗼𝗺𝗼𝗿𝗿𝗼𝘄 = 𝟳𝟬. #100DaysOfCode #LeetCode #MonotonicStack #GreedyAlgorithm #Algorithms #CodingInterview #Programming #Java #MediumLevel #PatternCombination #Day70Tomorrow
To view or add a comment, sign in
-
Day 56: Bit-Heavy Sorting 🔢 Problem 1356: Sort Integers by The Number of 1 Bits The mission: Sort an array based on the number of set bits (1s). If there’s a tie, the smaller number wins. The Strategy: • Custom Sort: Swapped a standard sort for a bit-count comparison. • Tie-Breaking: Used Integer.bitCount() to compare 1s, then fell back to raw values if counts were equal. • Bubble Logic: Implemented a nested loop to bubble the "heavier" bit counts to the end. Is O(n²) the fastest way? Definitely not. Does it get the green checkmark for today? Absolutely. Sometimes simple logic just hits different. 🚀 #LeetCode #Java #BitManipulation #Algorithms #Coding #DailyCode
To view or add a comment, sign in
-
-
🚀 Day 13/100 — Recursive Deconstruction Today’s Challenge: LeetCode 761 - Special Binary String 🧩 Yesterday was about bits; today was about Structural Grammar. Special Binary Strings aren't just 1s and 0s; they are a language. The Realization: A special string is like a valid set of parentheses. 1 is ( and 0 is ). To find the "lexicographically largest" version, you have to break the string into its most basic components, sort them, and put them back together. My 0ms Optimization Strategy: 1. Mental Mapping: Treated the string as a nested structure. If 1...0 is a special string, I stripped the outer layer, solved the inside, and then re-wrapped it. 2. Greedy Reassembly: Used Collections.sort() with a reverse comparator. In binary, "larger" just means the 1s appear as early as possible. 3. Memory Management: Minimized object creation by identifying "split points" first before committing to substring allocations. Reflection: Sometimes the fastest way to solve a problem isn't to iterate forward, but to look at the problem as a recursive tree. Every "Special" chunk is a node that can be reordered to maximize the total value. 13 days down. The logic is getting deeper. 87 days to go. 🛠️ #Java #DSA #LeetCode #100DaysOfCode #Recursion #StringAlgorithms #SoftwareEngineer #CodingJourney #Optimization
To view or add a comment, sign in
-
-
🚀 Day 46 of #100DaysOfCode Solved 719. Find K-th Smallest Pair Distance on LeetCode 📏 🧠 Key Insight: We need to find the k-th smallest absolute difference among all possible pairs in the array. Brute force (generating all pairs) would be O(n²) → not efficient. Instead, we use Binary Search on Answer + Two Pointers. ⚙️ Approach: 1️⃣ Sort the array 2️⃣ Define search space: 🔹left = 0 (minimum distance) 🔹right = max(nums) - min(nums) 3️⃣ Apply Binary Search: 🔹For a given mid, count how many pairs have distance ≤ mid 4️⃣ Counting pairs efficiently: 🔹Use two pointers 🔹For each i, move j while nums[j] - nums[i] ≤ mid 🔹Add (j - i - 1) to count 5️⃣ If count ≥ k → try smaller distance 6️⃣ Else → increase distance 🎯 Final answer = smallest distance satisfying the condition ⏱️ Time Complexity: O(n log n + n log D) 🔹Sorting + Binary Search with linear check 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #TwoPointers #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 — 𝗪𝗵𝗲𝗻 𝗦𝘁𝗮𝗰𝗸𝘀 𝗚𝗲𝘁 𝗚𝗿𝗲𝗲𝗱𝘆 Yesterday: Valid Parentheses. Basic stack. Today: Remove Duplicate Letters. Stack + greedy + frequency tracking. Level up. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟯𝟭𝟲: Remove Duplicate Letters (Medium) 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲: Remove duplicate letters so the result is: Smallest in lexicographical order (dictionary order) Contains each letter exactly once Example: "bcabc" → "abc" The trick? Knowing when to remove a character from the stack to make room for a better one. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: Monotonic stack + greedy decisions. 👉 Track frequency: how many times each character appears ahead 👉 Track visited: have we already used this character? 👉 For each character, pop stack if: Stack top is larger (worse for lexicographical order) Stack top appears again later (we can use it then) 👉 Push current character and mark visited Time: O(n), Space: O(1) — only 26 letters max. 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: This combines three patterns: Monotonic stack (maintaining order) Greedy algorithm (making locally optimal choices) Frequency tracking (knowing what's ahead) Solving it wasn't about knowing one technique. It was about combining them. 𝗖𝗼𝗱𝗲: https://lnkd.in/gzw6ACFr 57 down. 43 to go. 𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #MonotonicStack #GreedyAlgorithm #DataStructures #Algorithms #CodingInterview #Programming #Java #MediumLevel #PatternCombination
To view or add a comment, sign in
-
🚀 Day 76 / 100 Days of LeetCode Challenge 🧠 Today’s problem: Minimum Operations to Transform a Binary String I worked on a problem that required counting the number of 0s in a binary string and determining the minimum operations needed based on a given integer k. 📌 Problem intuition: Count the number of zeros in the string. If there are no zeros → already valid → return 0. If the string length equals k and all characters are zeros → 1 operation needed, otherwise -1. Otherwise, compute hase = len - k and proceed with further logic (though the implementation is still in progress). 🔍 Key takeaway: Sometimes the simplest approach—like counting zeros—can form the foundation of a solution. But edge cases like len == k and zero-count logic must be handled carefully to avoid incorrect results. 💻 Code snippet highlights: Loop through string, using bitwise operation ~s.charAt(i) & 1 to count zeros efficiently. Early returns for edge cases. Still refining the full logic, but proud of the progress! 📈 Runtime: 4 ms (Beats 99.66%) 💾 Memory: 47.68 MB (Beats 87.62%) ✅ 999/999 test cases passed #100DaysOfCode #LeetCode #CodingChallenge #Java #ProblemSolving #Algorithms #DataStructures #DeveloperJourney #TechCommunity #DailyCoding #CodeNewbie #WomenInTech #Programming #DevLife #LearnToCode #CodingLife #SoftwareEngineering #Tech #GrowthMindset
To view or add a comment, sign in
-
-
🚀 Day 525 of #750DaysOfCode 🚀 💡 LeetCode 1980: Find Unique Binary String Today’s problem was an interesting one involving binary strings and a clever observation. We are given n unique binary strings of length n, and the task is to return any binary string of length n that does not exist in the given array. 🔎 Approach I Used I applied a concept similar to Diagonalization. Traverse the array from 0 → n-1 Look at the i-th character of the i-th string Flip it (0 → 1 or 1 → 0) Append the flipped character to build a new string This guarantees the newly created string differs from every string in the list at least at one position, ensuring it is unique and not present in the array. ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(n) This approach works efficiently because the constraints are small and we only need to ensure one position difference with each string. Problems like this highlight how a simple observation can eliminate brute force completely. #leetcode #coding #programming #java #datastructures #algorithms #problemSolving #developer #codingchallenge #750DaysOfCode
To view or add a comment, sign in
-
-
Day 70 - LeetCode Journey Solved LeetCode 88: Merge Sorted Array (Easy) today — a classic problem that focuses on array manipulation and the two-pointer technique. The challenge is to merge two sorted arrays into one sorted array without using extra space, by modifying nums1 in-place. 💡 Core Idea: Instead of merging from the beginning, we start from the end of the arrays. Why? Because nums1 already has extra space at the end to accommodate elements from nums2. We use three pointers: • i → last valid element in nums1 • j → last element in nums2 • k → last position of merged array At each step, we place the larger element at position k, ensuring the array remains sorted. ⚡ Key Learning Points: • Using the two-pointer technique from the end • Performing in-place array merging • Maintaining O(m + n) time complexity • Avoiding unnecessary extra space This problem is a great reminder that sometimes changing the direction of traversal makes the solution much simpler. ✅ Better understanding of array manipulation ✅ Stronger grasp of two-pointer techniques ✅ Improved problem-solving efficiency Small problems like this build strong fundamentals for bigger challenges 🚀 #LeetCode #DSA #Java #TwoPointers #Arrays #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱𝟲/𝟭𝟬𝟬 — 𝗕𝗮𝗰𝗸 𝘁𝗼 𝘁𝗵𝗲 𝗖𝗹𝗮𝘀𝘀𝗶𝗰𝘀 Day 56. Valid Parentheses. The problem everyone sees on Day 1 of learning stacks. Except this time? I actually get why it works. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟮𝟬: Valid Parentheses (Easy) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a string of brackets: (), {}, []. Check if they're properly matched and nested. Examples: "()" → Valid "([)]" → Invalid (wrong order) "{[]}" → Valid (properly nested) 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: Stack. That's it. Push opening brackets. When you see a closing bracket, check if it matches the stack top. If yes, pop. If no, invalid. Empty stack at the end = valid. First time I saw this problem, I thought "why use a stack?" Now I see it—LIFO matches the nesting structure perfectly. 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: This isn't just about parentheses. It's about recognizing when a problem needs LIFO behavior. Compilers use this. Code editors use this. Expression parsing uses this. Pattern recognition >> memorization. 𝗖𝗼𝗱𝗲: https://lnkd.in/gdCu84Ja 56 down. 44 to go. 𝗗𝗮𝘆 𝟱𝟲/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #DataStructures #Algorithms #ProblemSolving #CodingInterview #Programming #Java #PatternRecognition
To view or add a comment, sign in
-
🚀 Day 514 of #750DaysOfCode 🚀 Today’s problem: 1022. Sum of Root To Leaf Binary Numbers 🌳 This was a really nice combination of: Binary Trees DFS Traversal Bit Manipulation 🧠 Problem Insight Each root-to-leaf path in the binary tree represents a binary number. Example path: 1 → 0 → 1 Represents binary 101 = 5 Instead of storing binary as a string and converting later ❌ We can build the number directly while traversing: current = current * 2 + node.val This works because: Multiplying by 2 shifts bits left Adding node value appends the current bit Clean. Efficient. Interview-friendly. ✅ ⏱ Complexity Time: O(N) Space: O(H) (recursion stack) 💡 Key Takeaway When dealing with binary paths: 👉 Think in terms of bit shifting, not string building. Small optimization. Big impact in interviews. Consistency > Motivation. 514 days done. Still building. 💪 #LeetCode #Java #BinaryTree #DFS #BitManipulation #DataStructures #CodingJourney #Consistency
To view or add a comment, sign in
-
Explore related topics
- Leetcode Problem Solving Strategies
- Approaches to Array Problem Solving for Coding Interviews
- LeetCode Array Problem Solving Techniques
- Java Coding Interview Best Practices
- Tips for Coding Interview Preparation
- Strategies for Solving Algorithmic Problems
- Common Data Structure Questions
- Why Use Coding Platforms Like LeetCode for Job Prep
- Common Algorithms for Coding Interviews
- Common Coding Interview Mistakes to Avoid
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