🚀 Day 503 of #750DaysOfCode 🔥 LeetCode 3714 — Longest Balanced Substring II (Medium) Today’s challenge was all about prefix differences, hashing, and smart optimization. We’re given a string containing only 'a', 'b', and 'c'. A substring is called balanced if all distinct characters in that substring appear the same number of times. 🧠 Key Insight Since we only have 3 characters, instead of brute-forcing every substring (which would be O(n²) ❌), we can: Track prefix counts of a, b, c Store frequency differences Use a HashMap to detect when the same difference pair appears again If two prefixes have the same difference values: (countA - countB) (countB - countC) Then the substring between them is balanced ✅ 💡 Optimization Strategy This solution smartly breaks the problem into 3 cases: 1️⃣ Only one distinct character 2️⃣ Exactly two distinct characters 3️⃣ All three characters Each case is handled efficiently using prefix differences and HashMaps. Time Complexity: O(n) Space Complexity: O(n) No brute force. No nested loops. Just pure prefix logic. 🔥 What I Learned Today ✔ Prefix difference technique is powerful ✔ Hashing states helps avoid O(n²) brute force ✔ Breaking complex problems into structured cases simplifies thinking ✔ Medium problems often test observation, not complexity “The difference between good and great coders is not syntax — it’s pattern recognition.” Onward to Day 504 💪🔥 #LeetCode #DataStructures #Algorithms #Java #CodingJourney #750DaysOfCode #ProblemSolving #TechGrowth
LeetCode 3714: Longest Balanced Substring II
More Relevant Posts
-
🚀 LeetCode Daily Challenge Today’s problem was Find Unique Binary String. Problem: We are given n unique binary strings of length n. The task is to return any binary string of length n that does not exist in the given array. At first glance, brute force might come to mind: Generate all 2^n binary strings and check which one is missing. But that’s inefficient. 💡 Better Idea – Diagonal Trick (Inspired by Cantor’s Diagonal Argument) Instead of checking all possibilities, we construct a new string by flipping the i-th bit of the i-th string. The new string will differ from every string in the array at least at one position. Therefore, it cannot match any existing string. ⚡ Time Complexity: O(n) ⚡Space Complexity: O(n) 🔍 Key Insight: By flipping the diagonal bits, we guarantee the generated binary string is different from every given string. Problems like this remind me that a simple mathematical idea can lead to a really elegant algorithm. #LeetCode #DailyCodingChallenge #Java #Algorithms #DataStructures #ProblemSolving #CodingInterview
To view or add a comment, sign in
-
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 1784.Check if Binary String Has At Most One Segment of Ones Today’s challenge was a short but insightful problem that demonstrates how recognizing patterns in a binary string can lead to an extremely elegant solution. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐮𝐦𝐦𝐚𝐫𝐲: We are given a binary string s consisting only of '0' and '1'. The task is to determine whether the string contains at most one continuous segment of '1'. In other words, once a '0' appears after a '1', there should not be another '1' later in the string. If there is more than one segment of '1', return false. Otherwise, return true. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: The key observation is very simple. If a binary string has more than one segment of '1', the pattern "01" must appear somewhere in the string. Why? A valid string with only one segment of ones looks like: 0001111000 But an invalid string with multiple segments would look like: 11100111 Notice the transition "01" which indicates that ones started again after a zero. Therefore, the entire problem reduces to checking whether the substring "01" exists. If "01" is present → there are multiple segments of '1'. If "01" is absent → there is at most one segment. This allows us to solve the problem in a single line using the built-in string function. 𝐂𝐨𝐝𝐞 𝐒𝐧𝐢𝐩𝐩𝐞𝐭 (Java): class Solution { public boolean checkOnesSegment(String s) { return !s.contains("01"); } } 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 𝐀𝐧𝐚𝐥𝐲𝐬𝐢𝐬: Time Complexity: O(n) Space Complexity: O(1) We scan the string once to check if the pattern "01" exists. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Sometimes the best solution is identifying a simple pattern instead of simulating the entire process. Understanding how transitions occur in binary strings can simplify many problems. Leveraging built-in string functions can make solutions both clean and efficient. A great reminder that even small problems can reinforce strong pattern-recognition skills. #LeetCode #DSA #Java #ProblemSolving #Algorithms #BinaryStrings #CodingPractice #Consistency #100DaysOfCode #LearningJourney
To view or add a comment, sign in
-
-
🚀 Day 13/100 — Recursive Deconstruction Today’s Challenge: LeetCode 761 - Special Binary String 🧩 Yesterday was about bits; today was about Structural Grammar. Special Binary Strings aren't just 1s and 0s; they are a language. The Realization: A special string is like a valid set of parentheses. 1 is ( and 0 is ). To find the "lexicographically largest" version, you have to break the string into its most basic components, sort them, and put them back together. My 0ms Optimization Strategy: 1. Mental Mapping: Treated the string as a nested structure. If 1...0 is a special string, I stripped the outer layer, solved the inside, and then re-wrapped it. 2. Greedy Reassembly: Used Collections.sort() with a reverse comparator. In binary, "larger" just means the 1s appear as early as possible. 3. Memory Management: Minimized object creation by identifying "split points" first before committing to substring allocations. Reflection: Sometimes the fastest way to solve a problem isn't to iterate forward, but to look at the problem as a recursive tree. Every "Special" chunk is a node that can be reordered to maximize the total value. 13 days down. The logic is getting deeper. 87 days to go. 🛠️ #Java #DSA #LeetCode #100DaysOfCode #Recursion #StringAlgorithms #SoftwareEngineer #CodingJourney #Optimization
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 — 𝗪𝗵𝗲𝗻 𝗦𝘁𝗮𝗰𝗸𝘀 𝗚𝗲𝘁 𝗚𝗿𝗲𝗲𝗱𝘆 Yesterday: Valid Parentheses. Basic stack. Today: Remove Duplicate Letters. Stack + greedy + frequency tracking. Level up. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟯𝟭𝟲: Remove Duplicate Letters (Medium) 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲: Remove duplicate letters so the result is: Smallest in lexicographical order (dictionary order) Contains each letter exactly once Example: "bcabc" → "abc" The trick? Knowing when to remove a character from the stack to make room for a better one. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: Monotonic stack + greedy decisions. 👉 Track frequency: how many times each character appears ahead 👉 Track visited: have we already used this character? 👉 For each character, pop stack if: Stack top is larger (worse for lexicographical order) Stack top appears again later (we can use it then) 👉 Push current character and mark visited Time: O(n), Space: O(1) — only 26 letters max. 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: This combines three patterns: Monotonic stack (maintaining order) Greedy algorithm (making locally optimal choices) Frequency tracking (knowing what's ahead) Solving it wasn't about knowing one technique. It was about combining them. 𝗖𝗼𝗱𝗲: https://lnkd.in/gzw6ACFr 57 down. 43 to go. 𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #MonotonicStack #GreedyAlgorithm #DataStructures #Algorithms #CodingInterview #Programming #Java #MediumLevel #PatternCombination
To view or add a comment, sign in
-
🚀 A quick tip from the “Rotate String” problem! A popular trick to check if one string is a rotation of another is this: return s.length() == goal.length() && (s + s).contains(goal); The idea is pretty straightforward: if goal is a rotation of s, it’ll show up somewhere inside s + s. For example: s = "abcde" goal = "cdeab" s + s = "abcdeabcde" and you can see "cdeab" right there. But here’s the catch: the .contains() method does a substring search that can be slow in the worst case (up to O(n²)). So while this trick usually works fine, it’s not the most efficient for all cases. But to reduce the time complexity, we can use KMP (Knuth–Morris–Pratt) algorithm instead: Build the LPS (Longest Prefix Suffix) array for goal Use KMP to search for goal inside s + s This guarantees the search will run in O(n) time. 💡 It’s a good reminder that a simple trick can solve a problem quickly, but knowing classic algorithms like KMP helps to build solutions that perform well even in tricky situations. #Algorithms #Coding #TechTips #Java #LeetCode #SoftwareEngineering
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 | 𝗖𝗼𝗻𝘃𝗲𝗿𝘁 𝗕𝗶𝗻𝗮𝗿𝘆 𝗡𝘂𝗺𝗯𝗲𝗿 𝗶𝗻 𝗔 𝗟𝗶𝗻𝗸𝗲𝗱 𝗟𝗶𝘀𝘁 𝘁𝗼 𝗜𝗻𝘁𝗲𝗴𝗲𝗿 Day 48 ✅ — Binary logic meets linked list traversal. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟭𝟮𝟵𝟬: Convert Binary Number in a Linked List to Integer (Easy) From LeetCode 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Each node represents a binary digit (0 or 1). The head is the most significant bit. So this isn’t just a linked list problem — it’s a 𝗯𝗶𝗻𝗮𝗿𝘆 𝘁𝗼 𝗱𝗲𝗰𝗶𝗺𝗮𝗹 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻 problem wrapped inside linked list traversal. Two key steps: 1️⃣ Compute the length of the list 2️⃣ Traverse again and apply positional binary weights (2^(length-1)) If a node contains 1, add 2^(position) to the result. Classic bit-weight logic. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 First pass: calculate linked list length 👉 Second pass: • If node value is 1 → add 2^(length-1) • Decrement length 👉 Handle last node separately Time Complexity: O(n) Space Complexity: O(1) Two traversals, constant space, clean logic. 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: This problem reinforces something important: Linked list questions aren’t always about pointer manipulation. Sometimes they test whether you can layer fundamental concepts (like binary math) on top of traversal mechanics. The more I practice, the clearer patterns become: Traversal is routine. The real thinking is in identifying the hidden concept. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g9TyHZGk 𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 ✅ | 𝟱𝟮 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #Binary #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #TimeComplexity #Programming
To view or add a comment, sign in
-
Sometimes a brute force approach is more than enough before jumping to complex optimizations. 🚀 While solving LeetCode 3713 – Longest Balanced Substring, I followed a straightforward brute-force strategy that worked effectively within constraints. 🔍 Approach: The idea is to check every possible substring and verify whether it is balanced. A substring is considered balanced if all characters present in it appear the same number of times. 1. Iterate through every possible starting index i. 2. For each start, extend the substring with index j. 3. Maintain a frequency array of size 26 to track occurrences of characters. 4. After each extension, check whether all non-zero frequencies are equal. 5. If the substring is balanced, update the maximum length. (The helper function verifies the balance condition by ensuring all characters that appear in the substring have the same frequency.) ⏱ Time Complexity: >Outer loop for start index → O(n) >Inner loop for end index → O(n) >Balance check over 26 characters → O(26) ≈ O(1) >Overall Time Complexity: O(n²) 💾 Space Complexity: >Frequency array of size 26 → O(1) (constant space) ✨ Sometimes the simplest idea—checking all possibilities carefully—can solve the problem efficiently without overcomplicating the solution. #LeetCode #ProblemSolving #DataStructures #Algorithms #Java #CodingJourney
To view or add a comment, sign in
-
-
Day 8/60 – LeetCode Challenge 🔥 Problem: Longest Substring Without Repeating Characters Today’s concept: Sliding Window + HashMap 💡 The question is: Given a string s, find the length of the longest substring without repeating characters. Brute force approach? Generate all substrings and check duplicates → O(n²) ❌ Better approach? Use Sliding Window with a HashMap. Intuition (Based on My Code): I used two pointers: • low → start of the window • high → end of the window As high moves forward: • I add the character to the HashMap • The map stores character frequency Now here’s the important observation: If there are no duplicates in the current window, then: map.size() == window length But if a duplicate exists, then: map.size() < window length So while map.size() < (high - low + 1), I shrink the window by: • Reducing frequency of s.charAt(low) • Removing it from map if frequency becomes 0 • Moving low forward After the window becomes valid again, I update the result with: res = max(res, current window length) Why this works? Because: • Each character enters the window once • Each character leaves the window once So the total complexity stays linear. Time Complexity: O(n) Space Complexity: O(n) Key Learning: When the question involves: • Longest substring • No repeating characters • Continuous segment Think → Sliding Window + Hashing Day 8 completed ✅ Consistency continues 🚀 #LeetCode #DSA #SlidingWindow #HashMap #Java #CodingChallenge #PlacementPreparation
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟰𝟰/𝟭𝟬𝟬 | 𝗗𝗲𝗹𝗲𝘁𝗲 𝗡𝗼𝗱𝗲 & 𝗥𝗲𝗺𝗼𝘃𝗲 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 Day 44 ✅ — Two deletion techniques, one day. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺𝘀: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟯𝟳: Delete Node in a Linked List (Medium) ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟬𝟯: Remove Linked List Elements (Easy) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: 𝕯𝖊𝖑𝖊𝖙𝖊 𝕹𝖔𝖉𝖊 (#237): The twist? You're only given the node to delete—not the head. Can't access previous node. Solution: Copy next node's value to current, then skip next. Clever trick that feels like breaking the rules. 𝕽𝖊𝖒𝖔𝖛𝖊 𝕰𝖑𝖊𝖒𝖊𝖓𝖙𝖘 (#203): Remove all nodes with a specific value. The standard deletion problem—handle head separately, then traverse and skip matching nodes. Two problems, two deletion strategies. Both are in-place, O(1) space. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵𝗲𝘀: 𝑫𝒆𝒍𝒆𝒕𝒆 𝑵𝒐𝒅𝒆: 👉 Copy next node's value to current 👉 Skip next node: node.next = node.next.next 𝑹𝒆𝒎𝒐𝒗𝒆 𝑬𝒍𝒆𝒎𝒆𝒏𝒕𝒔: 👉 Handle head nodes with target value first 👉 Traverse and skip nodes matching value 👉 Return potentially new head Time: O(n), Space: O(1) for both 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Fifteen linked list problems (Day 30-44). Deletion patterns are now second nature. The "Delete Node" trick shows that sometimes the clever solution isn't what you'd expect. Challenge your assumptions. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/gMb9bPNy 𝗗𝗮𝘆 𝟰𝟰/𝟭𝟬𝟬 ✅ | 𝟱𝟲 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #NodeDeletion #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #InPlaceAlgorithm #Programming #ProblemSolving
To view or add a comment, sign in
-
𝗗𝗮𝘆 𝟲𝟲/𝟭𝟬𝟬 — 𝗘𝘃𝗮𝗹𝘂𝗮𝘁𝗲 𝗥𝗲𝘃𝗲𝗿𝘀𝗲 𝗣𝗼𝗹𝗶𝘀𝗵 𝗡𝗼𝘁𝗮𝘁𝗶𝗼𝗻 Day 66. Two-thirds done. Today's problem? The reason calculators exist. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟭𝟱𝟬: Evaluate Reverse Polish Notation (Medium) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Evaluate expressions in Reverse Polish Notation (RPN). Numbers come first, operators after. Example: ["2","1","+","3","*"] → ((2+1)*3) = 9 No parentheses needed. No order of operations confusion. Just read left to right. 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: Stack. When you see a number, push it. When you see an operator, pop two numbers, apply the operation, push the result. The last number standing? That's your answer. Used ArrayList as a stack for cleaner syntax with removeLast(). Same LIFO behavior. 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: This is how calculators and compilers evaluate expressions internally. RPN eliminates ambiguity. "3 + 4 * 5" needs rules. "3 4 5 * +" doesn't. Understanding this makes you understand how expression parsing works. 𝗖𝗼𝗱𝗲: https://lnkd.in/gXwcqVdP 𝗗𝗮𝘆 𝟲𝟲/𝟭𝟬𝟬 ✅ 𝟲𝟲 𝗱𝗼𝘄𝗻. 𝟯𝟰 𝘁𝗼 𝗴𝗼. #100DaysOfCode #LeetCode #Stack #RPN #Algorithms #ExpressionEvaluation #CodingInterview #Programming #Java #MediumLevel #CompilerDesign
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