Check if Binary Codes Exist in String with Hashing and Sliding Window

🔥 Day 513 of #750DaysofCode 🔥 LeetCode 1461 | Check If a String Contains All Binary Codes of Size K Today’s problem was a really interesting mix of strings + hashing + sliding window + bit manipulation. 🧠 Problem Summary: Given a binary string s and an integer k, return true if every possible binary code of length k exists as a substring of s. For example: Input: s = "00110110", k = 2 All possible binary codes of size 2 → "00", "01", "10", "11" Since all exist → ✅ true 💡 Key Insight For length k, total possible binary codes = 2^k So instead of generating all binary codes explicitly, we: Slide a window of size k across the string Store each substring in a HashSet If set.size() == 2^k, we return true ⚡ Optimized Thinking Since: k <= 20 2^20 = 1,048,576 We can also use: Rolling hash Bitmask technique Integer encoding instead of substring creation 🎯 What I Learned Today ✔ Instead of generating all combinations, track what appears ✔ Bitmasking can optimize substring problems ✔ Sliding window + hashing is powerful for pattern coverage problems ✔ Always check constraints before choosing brute force Consistency > Motivation 💪 Onwards to Day 514 🚀 #750DaysOfCode #LeetCode #Java #DSA #CodingJourney #BitManipulation #SlidingWindow

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories