Solved LeetCode 3234: Count Dominant Ones Substrings

šŸš€ Solved LeetCode 3234: Count Substrings with Dominant Ones! 🧠 Just tackled a challenging substring problem: 3234. Count the Number of Substrings With Dominant Ones. The goal is to find all substrings where the count of '1's is greater than or equal to the square of the count of '0's. A simpleĀ O(N²)Ā approach is too slow, so a more optimized strategy is needed. My Approach: Key Insight:Ā The conditionĀ ones >= zeros² means the number of zeros in any valid substring is small (at most √N). This is the key to optimization. Fix the Endpoint:Ā I iterate through the string, fixing theĀ endĀ of the potential substring. "Zero-Hopping" Technique:Ā From theĀ end, I iterate backward, but instead of going one by one, I "hop" from each '0' to the previous '0'. A precomputed array makes this hopĀ O(1). Efficient Counting:Ā At each hop, I calculate the number of valid start positions in the segment between the two zeros, bringing the total time complexity down toĀ O(N√N). This was a great puzzle in finding the bottleneck and building an algorithm around it! šŸ”—Ā Problem Link:Ā https://lnkd.in/gpmZdskg #LeetCode #Coding #Algorithm #Python #ProblemSolving #SoftwareEngineering #Optimization #LeetCode #Coding #Algorithm #Python #Programming #InterviewPrep

  • text

To view or add a comment, sign in

Explore content categories