🔥 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
Check if Binary Codes Exist in String with Hashing and Sliding Window
More Relevant Posts
-
🚀 Day 536 of #750DaysOfCode 🚀 Today I solved Count Submatrices With Equal Frequency of X and Y (LeetCode 3212) using Java. 🔹 Problem Summary: Given a grid containing 'X', 'Y', and '.', we need to count the number of submatrices starting from the top-left corner (0,0) such that: • The number of 'X' and 'Y' is equal • The submatrix contains at least one 'X' 🔹 Approach: Instead of checking every possible submatrix (which would be too slow), I used the Prefix Sum technique. I maintained two prefix matrices to store the count of 'X' and 'Y' up to each cell. For every position (i, j), I checked: countX == countY and countX > 0 If true, that submatrix is valid. This reduces the complexity to O(n × m), which works efficiently for large grids. 🔹 Key Concepts Learned: ✅ Prefix Sum in 2D ✅ Matrix traversal optimization ✅ Handling constraints up to 1000 × 1000 ✅ Clean implementation in Java Consistent practice is making problem-solving faster and more structured every day. #750DaysOfCode #Day536 #LeetCode #Java #DataStructures #Algorithms #PrefixSum #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 539 of #750DaysOfCode 🚀 Today I solved Determine Whether Matrix Can Be Obtained By Rotation (LeetCode 1886) using Java. 🔹 Problem Summary: Given two n × n binary matrices mat and target, we need to check whether mat can be converted into target by rotating it in 90-degree clockwise steps (0°, 90°, 180°, or 270°). 🔹 Approach Used: I followed a simple simulation approach: • Check if the current matrix equals target • If not, rotate the matrix by 90° clockwise • Repeat this up to 4 times • If any rotation matches, return true For rotation, I used the formula: new[j][n − 1 − i] = mat[i][j] This allowed me to generate the rotated matrix cleanly and compare it with the target. 🔹 Key Concepts Learned: ✅ Matrix rotation (90° clockwise) ✅ Matrix comparison ✅ Simulation approach ✅ Working with 2D arrays in Java ✅ Writing clean helper functions Practicing matrix problems daily is improving my understanding of transformations and indexing logic. #750DaysOfCode #Day539 #LeetCode #Java #DSA #Algorithms #CodingChallenge #Matrix #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 33 of Daily DSA 🚀 Solved LeetCode 58: Length of Last Word ✅ Problem: Given a string s, return the length of the last word (a sequence of non-space characters). Approach: First, trim() the string to remove trailing spaces Traverse the string from the end Count characters until a space is encountered 💡 Key Insight: Instead of splitting the string (which uses extra space), we can simply iterate backwards for an efficient solution. ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.17 MB A simple yet important problem to strengthen string traversal techniques. #DSA #LeetCode #Java #Strings #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 65: The "One-Liner" Win 🎯 Problem 1784: Check if Binary String Has at Most One Segment of Ones Today was a lesson in simplifying logic. The challenge: check if a binary string contains more than one contiguous segment of '1's, given that the string starts with '1'. The Strategy: • Observation: If there are multiple segments of '1's, they must be separated by at least one '0'. • The Pattern: In a string starting with '1', any "new" segment of ones would look like "01" somewhere in the string. • The Execution: A simple !s.contains("01") handles the entire check. Sometimes we hunt for complex algorithms when a single string method is the ultimate counter. Clean, readable, and passed all test cases. 🚀 #LeetCode #Java #StringManipulation #Coding #Efficiency #DailyCode
To view or add a comment, sign in
-
-
🚀 Day 95 of My 100 Days LeetCode Challenge | Java Today’s challenge was a grid traversal + simulation problem that required careful observation of patterns. The goal was to find the three largest rhombus border sums in a grid. Unlike regular submatrix problems, this one only considers the border elements of rhombus shapes, which makes the calculation a bit more tricky. The approach involved exploring each cell as a potential rhombus center, expanding possible rhombus sizes, and calculating the sum of the boundary elements while ensuring the rhombus stays within grid limits. A TreeSet helped efficiently keep track of the top three unique sums. ✅ Problem Solved: Get Biggest Three Rhombus Sums in a Grid ✔️ All test cases passed (117/117) ⏱️ Runtime: 217 ms 🧠 Approach: Grid Traversal + Simulation + TreeSet 🧩 Key Learnings: ● Grid problems often require careful boundary management. ● Visualizing shapes (like rhombuses) helps simplify implementation. ● TreeSet is useful for maintaining sorted unique values efficiently. ● Simulation-based solutions demand attention to detail. ● Not every grid problem is about rectangles — shapes matter too. This problem was a good reminder that geometric patterns in grids can lead to interesting algorithmic challenges. 🔥 Day 95 complete — improving my grid traversal and pattern-handling skills. #LeetCode #100DaysOfCode #Java #GridProblems #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 55 of #100DaysOfLeetCode 💻✅ Solved #434. Number of Segments in a String problem in Java. Approach: • Initialized a counter to track number of words (segments) • Traversed the string character by character • Checked for non-space characters • If the current character is not a space and either it is the first character or the previous character is a space, incremented the count • This ensures counting only the starting of each word Performance: ✓ Runtime: 0 ms (Beats 100% submissions) 🚀 ✓ Memory: 41.83 MB (Beats 99.81% submissions) Key Learning: ✓ Practiced string traversal without using extra space ✓ Learned how to identify word boundaries efficiently ✓ Improved logic building for handling spaces and edge cases Learning one problem every single day 🚀 #Java #LeetCode #DSA #Strings #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 14/30 – LeetCode streak Today’s problem: Concatenation of Consecutive Binary Numbers You need the decimal value of '1' + '2' + '3' + ... + 'n' written in binary back-to-back, all under mod (10^9 + 7). Core trick: treat “concatenate in binary” as shift + OR: * When you append 'i' to the right, you’re really shifting the current result left by 'bits(i)' and OR-ing 'i' into the free space. * The number of bits only increases when 'i' hits a power of two (1, 2, 4, 8, …), so you just track 'bits' and bump it whenever '(i & (i - 1)) == 0'. Day 14 takeaway: Once you see that “stick this binary to the right” is the same as “shift by its bit length and OR”, the whole problem becomes a clean for-loop plus the power-of-two trick—no string building or big integer juggling needed. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
Day 4/100 – LeetCode Challenge Problem: Valid Anagram Today’s problem focused on string comparison using frequency counting instead of sorting. Problem Statement: Given two strings s and t, determine if t is an anagram of s. An anagram means: Same characters Same frequency Order does NOT matter Approach (Optimized – O(n)): If lengths are different → return false Create an integer array of size 26 (for lowercase letters) Increment count for characters in s Decrement count for characters in t If all values are zero → valid anagram This ensures: Time Complexity: O(n) Space Complexity: O(1) (fixed array of size 26) Key Concepts Practiced: Hashing technique Character indexing using ASCII (char - 'a') Optimizing from sorting to counting Constant space optimization #100DaysOfCode #LeetCode #DSA #Java #CodingInterview #SoftwareEngineerJourney #ProblemSolving
To view or add a comment, sign in
-
-
Leetcode Problem || Sort Integers by Number of 1 Bits (1356)🚀 Today I worked on a problem where we sort integers based on: 1️⃣ Number of set bits in binary representation 2️⃣ If equal, sort by numerical value 💡 Learned: How comparator chaining works Importance of object boxing in Java sorting Clean way to handle multi-level sorting #Java #DSA #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Leetcode Problem || Number of Steps to Reduce a Number in Binary Representation to One(1404)🚀 Today I solved a binary manipulation problem where we must: If number is even → divide by 2 If odd → add 1 Count steps until number becomes 1 💡 Learned: How to simulate binary addition using carry Avoid integer overflow Optimize string-based numeric problems #Java #DSA #BitManipulation #LeetCode #ProblemSolving
To view or add a comment, sign in
-
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