🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g4J85qey 💡 My thought process: If you have a block of three 0s followed by a block of two 1s (00011), you can only create two valid pairs: 01 and 0011. The smaller group always limits the number of matches you can make, which is why the code uses min(prev, curr). To keep track of this, the code uses prev for the size of the block you just finished and curr for the block you’re currently counting. As you go through the string, you keep increasing curr as long as the characters remain the same. The moment the character changes, like when you hit a 1 after a series of 0s, it means you've completed a block. You take the min of the old block and the new block, add it to your total, move the curr count into prev, and start over for the next group. There is one small issue in the code: because that addition only happens when the characters change, the very last block in the string doesn't get processed inside the loop. That’s why there is one extra result += min(prev, curr) at the very end to catch that final transition. 👉 My Solution: https://lnkd.in/gdK35f76 If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
LeetCode Challenge: Counting 0s and 1s in a String
More Relevant Posts
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/grtpbGeG 💡 My thought process: A counter tracks the number of operations. The loop runs until the string length becomes 1, which means the number has been reduced to 1. If the last character of the string is 0, the number is even in binary. Dividing by 2 is the same as removing the last bit, so pop_back removes the trailing 0. If the last character is 1, the number is odd and needs to be increased by 1. To simulate binary addition, the code goes through the string from right to left. All consecutive trailing 1 characters are changed to 0 to manage the carry. When a 0 is found, it becomes 1, and the carry stops. If the loop finishes without finding a 0, it means the string was made entirely of 1 characters. Adding 1 then results in an extra leading 1, which is added by putting 1 at the front of the string. Each iteration counts as one step. This process continues until only one character is left. The function finally returns the total number of steps needed to reduce the binary number to 1. 👉 My Solution: https://lnkd.in/guc6fia6 If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gx6PQVMB 💡 My thought process: The brute force method checks all possible rotations of the binary string by moving the first character to the end repeatedly. For each rotated string, it counts the number of mismatches needed to create an alternating pattern that starts with 1. The opposite alternating pattern, which starts with 0, would require different changes. The method takes the minimum of the two counts for each rotation and returns the lowest value overall. The optimized method does not generate all rotations. Instead, it doubles the string and uses a sliding window of size n to represent every possible rotation. It keeps track of the mismatch count for forming an alternating pattern that starts with 1. As the window slides, it updates this count by subtracting the contribution of the character that leaves the window and adding the incoming character. At each step, it compares both alternating patterns to find the minimum number of flips needed. 👉 My Solution: https://lnkd.in/gP2FWVYP If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g6uRPkE6 💡 My thought process: The first approach calculates the bitwise complement by examining each bit of the number. It starts by determining how many bits are needed to represent n using log2(n) + 1. For each bit position i, a mask (1 << i) is used to isolate that bit from n. If the extracted bit is 0, the result for that position is set by adding the same mask, effectively flipping the bit. This creates a new number where all bits within the effective bit length of n are inverted, resulting in the complement. The second approach uses the XOR operation to flip all relevant bits at once. It first calculates the number of bits in n and creates a mask filled with 1s in all those positions using (1 << digits) - 1. This mask represents the highest value that can be formed with the same number of bits. Applying n ^ mask flips every bit within that range because XOR with 1 changes the bit and XOR with 0 leaves it the same, giving the bitwise complement of n. 👉 My Solution: https://lnkd.in/gGCdUW3H If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gDzrB9cW 💡 My thought process: This solution creates the binary string step by step, starting from "0". For each level from 2 to n, it forms a new string by taking the previous string, inserting "1" in the middle, and then adding the reversed and bit-flipped version of the previous string (0 changes to 1 and 1 changes to 0). After each construction, it checks if the string has reached or exceeded the required k position. If it has, it immediately returns the k-th character. This approach prevents any extra construction once the answer is found. 👉 My Solution: https://lnkd.in/gFXgkTpm If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g6C9kJb4 💡 My thought process: For a specific value of k, there are exactly 2^k possible binary strings of that length. For example, if k = 2, the possible binary codes are 00, 01, 10, and 11, making a total of 4 combinations. The solution uses a sliding window approach with size k. It goes through the string using index j and keeps adding characters to a temporary string called window. When the size of the window equals k, the substring is added to an unordered set to ensure it is unique. After adding, the window shifts forward by removing its first character using substr(1), and the process continues. At the end, the function checks if the number of unique substrings stored in the set equals 1 shifted left by k, which represents 2^k. If the sizes match, it means every possible binary substring of length k is in the string, and the function returns true. If not, it returns false. The time complexity is O(n × k) because each substring operation can take up to O(k) time. The space complexity is O(2^k) in the worst case for storing all possible substrings. 👉 My Solution: https://lnkd.in/gBrK8uHB If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/grkpkPnR 💡 My thought process: The approach uses a greedy strategy. First, for each row in the grid, we count the number of trailing zeros by scanning from the last column to the left until we encounter a 1. We store these counts in a separate array called endZero because only the number of trailing zeros matters for checking whether a row meets the upper triangular condition. For a valid grid, at row index i, we must ensure there are at least n - i - 1 trailing zeros. This way, all elements above the main diagonal can remain zero. We go through each row from top to bottom. If the current row has enough trailing zeros (endZero[i] >= n - i - 1), we proceed to the next row. If not, we look below for the nearest row that meets the requirement. If we cannot find such a row, arranging the grid is impossible, so we return -1. If we do find a valid row, we simulate adjacent row swaps by continually swapping it upward until it reaches position i, counting each swap. This method ensures we use the minimum number of swaps by always choosing the closest valid row. 👉 My Solution: https://lnkd.in/gK4-jM2B If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g5nYsHBR 💡 My thought process: To form a decimal number n using the fewest deci-binary numbers, the key factor is the highest digit in n. Each deci-binary number can contribute a maximum of 1 to any digit position. So, if a digit in n is 7, we need at least 7 deci-binary numbers to reach that sum. Thus, the minimum number of deci-binary numbers needed is simply the highest digit in the provided string. The approach is simple: start with a variable result set to 0. Go through each character in the string, convert it to an integer using n[i] - '0', update result with the greater value between result and digit, and finally return result. 👉 My Solution: https://lnkd.in/ggv3b48t If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g5ggMZxw 💡 My thought process: The approach checks how many mismatches occur if the string follows an alternating pattern starting with 0 (0,1,0,1...), counts the number of positions that violate this pattern, and since the opposite alternating pattern starting with 1 (1,0,1,0...) would require the remaining changes, the minimum of the two mismatch counts represents the minimum number of operations required. 👉 My Solution: https://lnkd.in/gVuWTeKh If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gJUJE-YA 💡 My thought process: The provided code uses a backtracking approach to generate all possible strings of length n that satisfy the happy string condition: no two adjacent characters can be identical. The algorithm builds these strings in lexicographical order by exploring 'a', 'b', and then 'c'. The main logic is in the solve function, which acts as a recursive explorer. It takes the current string and the number of characters left to add. At each step, it tries appending 'a', 'b', and 'c'. Before adding a character, it checks for safety by ensuring that the character is not the same as the last one in the string. This prevents violating the happy constraint. Once a character is added, the function calls itself with n-1 to fill the next position. After finishing that branch of the search, it removes the character to backtrack and try the next alphabetical option. In the getHappyString function, the process starts by launching three separate recursive chains: one with 'a', one with 'b', and one with 'c'. Since the loops and initial calls follow alphabetical order, the allStrings vector is filled with every valid combination in sorted order. Finally, the code checks if the requested index k exists within the total count of generated strings. If k is within bounds, it returns the string at index k-1; otherwise, it returns an empty string. 👉 My Solution: https://lnkd.in/gKDqj84r If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/geeKcuMP 💡 My thought process: The approach counts the number of special positions in a binary matrix by ensuring that a cell containing 1 is the only 1 in both its row and column. First, the number of rows, m, and columns, n, are determined. Two auxiliary arrays, rowOnes and colOnes, store the count of 1s in each row and column, respectively. The matrix is first traversed row by row to count how many 1s appear in each row and store these counts in rowOnes. Next, it is traversed column by column to count how many 1s appear in each column and store these counts in colOnes. After computing these counts, the matrix is scanned again. For every cell containing 1, it checks whether that row and column both contain exactly one 1. If both conditions are met, the position is considered special, and the count is increased. Finally, the total number of such positions is returned. 👉 My Solution: https://lnkd.in/g9eZdbF2 If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
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