Day 6/30 – LeetCode streak Today’s problem: Special Binary String This one finally felt like a proper "hard" — at first glance it’s just scary string juggling. What actually unlocked it for me was thinking of the string like balanced parentheses: - Treat '1' as "(" and '0' as ")". - Walk the string with a counter: '+1' on '1', '-1' on '0'. - Every time the counter comes back to '0', you’ve finished one smallest special chunk. Inside each such chunk, the outer '1' and '0' are basically fixed anchors. You strip them off, recursively make the inside as large as possible, then wrap it back as "1" + inner + "0". All those chunks go into a list, you sort them in descending order, then glue them together to get the lexicographically largest result. As for what felt hardest: Seeing those outer '1' and '0' as immovable anchors was the key. Once that clicked, recursion and "sort bigger chunks first" felt natural; before that, it just looked like raw string manipulation with no obvious starting point. #leetcode #dsa #java #recursion #consistency
LeetCode Day 6: Special Binary String Solution
More Relevant Posts
-
Day 22/30 – LeetCode streak Problem: Find Unique Binary String You are given 'n' distinct binary strings of length 'n' and must return any binary string of length 'n' that doesn’t appear in the array. Core idea (Cantor’s diagonal argument) * Think of 'nums' as an 'n × n' matrix of characters. * Look at the diagonal positions: character 'i' of string 'nums[i]'. * Build a new string where the 'i'-th character is the flip of 'nums[i].charAt(i)' ('0' → '1', '1' → '0'). * This constructed string differs from 'nums[0]' at index '0', from 'nums[1]' at index '1', …, from 'nums[i]' at index 'i', so it cannot be equal to any string in 'nums'. * Time complexity: 'O(n)', space: 'O(n)' for the output string. Day 22 takeaway: This problem is a neat, direct application of Cantor’s diagonalization: by flipping the diagonal bits, you guarantee a new string that is different from every given one in at least one position, without any searching or backtracking. #leetcode #dsa #java #strings #consistency
To view or add a comment, sign in
-
-
Day 17/30 – LeetCode streak Today’s problem: Find Kth Bit in Nth Binary String (1545). 'S₁ = "0"', and 'Sₙ = Sₙ₋₁ + "1" + reverse(invert(Sₙ₋₁))'. Length of 'Sₙ' is always '2ⁿ - 1', with a '1' sitting exactly in the middle. The structure looks like this: Sₙ = [Sₙ₋₁] + '1' + reverse(invert(Sₙ₋₁)) So for any position 'k': - Let 'mid = 2^(n-1)' (the middle index, 1-based). - If 'k == mid', answer is '1'. - If 'k < mid', it’s in the left half, which is exactly 'Sₙ₋₁' → recurse: 'findKthBit(n-1, k)'. - If 'k > mid', it’s in 'reverse(invert(Sₙ₋₁))'. Mirror it back into 'Sₙ₋₁' at 'mirror = 2*mid - k' (same as '2ⁿ - k'), recursively get that bit from 'Sₙ₋₁', then flip it. Day 17 takeaway: This is one of those problems where drawing the recursive construction once (left + 1 + reversed/inverted left) makes the “mid / mirror / invert” recursion feel natural—and you never have to actually build the string. #leetcode #dsa #java #recursion #consistency
To view or add a comment, sign in
-
-
Day 28/30 – LeetCode streak Problem: The k-th Lexicographical String of All Happy Strings of Length n A happy string is length 'n' over '{a,b,c}' with no two adjacent characters equal. Core idea * Total happy strings of length 'n': * First character: 3 choices ('a','b','c'). * Each of the remaining 'n-1' positions: 2 choices (anything different from the previous character). * So 'total = 3 * 2^(n-1)'. If 'k > total', the answer is an empty string. * Instead of generating all strings, we can index them directly: * Fixing the first character divides the list into blocks. * Each first character corresponds to a block of '2^(n-1)' strings. * Compute 'block = 1 << (n-1)'. * 'firstIdx = (k-1) / block' → '0 → a', '1 → b', '2 → c'. * 'rem = (k-1) % block' determines the remaining 'n-1' choices. * For each remaining position: * Look at the previous character. * The next character must be one of the two letters different from it, taken in lexicographic order. * Use bits of 'rem' to choose between the smaller or larger valid option. This lets us map 'k' directly to the correct happy string in 'O(n)' time without generating all combinations. Day 28 takeaway: Happy strings follow a clean combinatorial pattern of '3 * 2^(n-1)'. Treating the suffix as a binary decision sequence lets you construct the k-th string directly instead of using backtracking or BFS. #leetcode #dsa #java #strings #combinatorics #consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Practice Update Solved “1415. The k-th Lexicographical String of All Happy Strings of Length n” today. It’s an easy–medium problem for anyone comfortable with Backtracking, but a great exercise to strengthen recursive thinking and constraint-based generation. 🧠 Approach The idea is to generate all valid “happy strings” using backtracking and then simply pick the k-th string in lexicographical order. A happy string means: It only contains characters 'a', 'b', 'c' No two adjacent characters are the same ⚙️ Strategy 1. Start with an empty string and recursively build all possible strings of length n. 2. At each step, try adding characters from 'a' → 'c' to maintain lexicographical order. 3. Before adding a character, check the constraint: If the last character of the current string is the same as the character we want to add → skip it. 4. If the string length becomes n, add it to the list of valid happy strings. 5. After generating all valid strings: If k > total strings, return an empty string. Otherwise return the (k-1)th index from the list. 🔁 Key Backtracking Step After exploring a choice, we remove the last character to restore the previous state and try the next option. This pattern of choose → explore → undo (backtrack) is the heart of backtracking problems. 💡 Problems like this really show how powerful backtracking can be when generating all valid combinations under constraints. #LeetCode #Backtracking #Java #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 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 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
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-11 — Longest Substring Without Repeating Characters 🧩 Problem: Longest Substring Without Repeating Characters 💻 Platform: LeetCode (#3) This problem forced me to truly understand the Sliding Window pattern. Brute force checks every substring. That’s inefficient. Optimal approach: ✔ Use two pointers (left & right) ✔ Expand the window ✔ Shrink when duplicate appears ✔ Track max window size Time Complexity: O(n) Space Complexity: O(n) Big takeaway: If a problem mentions “longest substring” + “no repetition” → Sliding Window should be your first thought. Strings aren’t new logic. They’re just arrays of characters. Day-11 complete. Moving deeper into string patterns 🚀 #30DaysOfCode #LeetCode #DSA #Java #SlidingWindow #Algorithms #ProblemSolving #Developers
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 3 – LeetCode Practice Today, I solved the 3Sum problem on LeetCode: 🎯 Difficulty: Medium/Hard 💻 Language Used: Java 💡 Approach: • Given an integer array nums, the objective was to find all unique triplets that sum up to zero. • I sorted the array first to simplify searching and avoid duplicates. • Used a fixed pointer + two-pointer technique: – For each element (as the first of the triplet), used two pointers to find pairs that complete the sum to zero. • To avoid duplicate triplets, I skipped repeated values during iteration. ⏱ Complexity: • Time Complexity: O(n²) • Space Complexity: O(1) (excluding output list) 📚 Key Takeaway: This problem strengthened my understanding of sorting combined with two-pointer strategies to reduce brute-force complexity. Handling duplicates correctly was also key to returning unique triplets. Consistent problem-solving practice continues to improve my algorithmic thinking and confidence in handling array-based challenges. 💪 #LeetCode #Java #DSA #TwoPointers #Arrays #ProblemSolving #Algorithms #100DaysOfCode #SoftwareDeveloper
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