🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gXwweaP3 💡 My thought process: This solution finds the longest substring where the characters 'a', 'b', and 'c' appear the same number of times. It approaches the problem by breaking it into three separate cases: substrings with one character, substrings with two distinct characters, and substrings with all three characters. In the single-character case, the method scans the string and tracks the length of the longest continuous run of the same character. This addresses substrings where balance is easily met, as only one character is present. For the two-character case, the technique uses a prefix-difference approach. While scanning the string, it counts the two selected characters and calculates their difference. If the same difference has occurred before, the segment between the last index and the current index must have equal counts of the two characters, making its length a valid option. When a third character appears, it resets the counts and the hash map to prevent invalid matches across segments. In the three-character case, the prefix concept is extended to two dimensions. Instead of tracking one difference, the algorithm focuses on two differences: (count(a) − count(b)) and (count(a) − count(c)). If the pair of differences appears again, it indicates that the counts of all three characters between those indices are equal, creating a balanced substring. A hash map keeps the earliest index for each difference pair, allowing for quick length updates in a single pass through the string. 👉 My Solution: https://lnkd.in/gc_vMxFG 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 Daily Challenge: Longest Substring with Equal Character Counts
More Relevant Posts
-
🚀 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
-
-
𝗗𝗮𝘆 40 𝗼𝗳 𝟯𝟲𝟬 days of LeetCode Challenge. 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 :- 3714. Longest Balanced Substring II You are given a string s consisting only of the characters 'a', 'b', and 'c'. A substring of s is called balanced if all distinct characters in the substring appear the same number of times. Return the length of the longest balanced substring of s. 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗹𝗶𝗻𝗸:- https://lnkd.in/dqJiZzXm 𝗛𝗶𝗻𝘁:- 1. Break the problem by number of distinct characters: handle single-character runs, then for two characters use prefix-difference (count₁ − count₂) with a hashmap, and for three characters track two independent differences. 2. Whenever the same difference state reappears, the substring between those indices has equal counts, so update the maximum length. 𝗚𝗶𝘁𝗵𝘂𝗯 𝗰𝗼𝗱𝗲:- https://lnkd.in/dHNThed3 #SoftwareEngineering #DSAJourney #Coding #LeetCode #DSA #TechCareers #SDE
To view or add a comment, sign in
-
-
🚀 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
To view or add a comment, sign in
-
-
🚀 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/gwdQDKx4 💡 My thought process: The code calculates the sum of all root-to-leaf paths in a binary tree where each node contains either 0 or 1. Each path from root to leaf represents a binary number. The goal is to convert these binary numbers to decimal and return their total sum. The helper function solve performs a depth-first search. It builds a binary string along the current path by adding the node’s value. When a leaf node is reached, the binary string is saved in a vector. The recursion continues for the left and right children until all root-to-leaf paths are explored. In sumRootToLeaf, after collecting all binary path strings, each string is converted to its decimal equivalent using bit shifting. For each character, its value is multiplied by the corresponding power of 2 and added to a running total. The final result is the sum of all converted values. The time complexity is linear based on the number of nodes, and additional space is needed for recursion and storing path strings. 👉 My Solution: https://lnkd.in/gwAHXfX6 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/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
-
-
𝗗𝗮𝘆 50 𝗼𝗳 𝟯𝟲𝟬 days of LeetCode Challenge. 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 :- 1461. Check If a String Contains All Binary Codes of Given a binary string s and an integer k, return true if every binary code of length k is a substring of s. Otherwise, return false. 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗹𝗶𝗻𝗸:- https://lnkd.in/gDfaVQby 𝗛𝗶𝗻𝘁:- Don't try to solve the problems with the steps it required, instead try to find the unique substrings with length k and match with expected substring 2 ^ k. 𝗚𝗶𝘁𝗵𝘂𝗯 𝗰𝗼𝗱𝗲:- https://lnkd.in/gCa-GdEc #SoftwareEngineering #DSAJourney #Coding #LeetCode #DSA #TechCareers #SDE
To view or add a comment, sign in
-
-
🚀 Today’s LeetCode Problem — Sort Integers by Number of 1 Bits Let’s solve today’s LC in story mode 🎭 Imagine numbers are contestants in a Binary Costume Contest. Today’s contestants: 👉 [5, 3, 7] But this contest has a twist 👇 👨⚖️ The judge doesn’t care about the number itself first. He cares about… 🎈 How many 1s are in their binary costume 🎭 What Are They Wearing? 🔹 5 → 101 → 🎈🎈 (2 ones) 🔹 3 → 011 → 🎈🎈 (2 ones) 🔹 7 → 111 → 🎈🎈🎈 (3 ones) 🏆 Judge’s Rules 1️⃣ Fewer 1s → Higher priority 2️⃣ Same number of 1s → Smaller number wins 👉 Step 1: 3 & 5 → 2 ones 7 → 3 ones So 7 goes last. 👉 Step 2: Tie between 3 and 5 Smaller number first → 3 before 5 ✅ Final Answer: [3, 5, 7] 💡 How I Solved It I implemented this using: 🔹 Brian Kernighan’s Algorithm for efficient bit counting 🔹 TreeMap to maintain sorted order by bit count + value 🔹 Also compared with built-in bitCount() + custom comparator sorting This ensures: ✔ Efficient bit counting ✔ Clean tie-breaking ✔ Optimal sorting logic If you’re interested, I’ve pushed the complete solution (Kernighan + TreeMap + built-in approach) : https://lnkd.in/gV9HZ9w4 👨💻 Would love feedback from fellow developers 🙌 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #100DaysOfCode #Tech #SoftwareEngineering
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