Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 3714 | Longest Balanced Substring II | Java | Medium | Prefix Sum | CodeByTushu

Video information, playback and saved actions.

LeetCode 3714 | Longest Balanced Substring II | Java | Medium | Prefix Sum | CodeByTushu

Description: πŸš€ Welcome Back to CodeByTushu!

In today’s video, we solve LeetCode 3714 β€” Longest Balanced Substring II using String, Hash Table, and Prefix Sum in Java.

We are given a string named s containing up to 100,000 characters, consisting only of the letters a, b, and c. We need to find the length of the longest substring where every distinct character appears the same number of times.

Since the alphabet contains only three characters, a balanced substring can contain one, two, or three distinct characters. We divide the problem into three cases. The first case involves one distinct character and can be solved by finding the longest continuous block of identical characters. The second case involves two distinct characters, where the third character is absent and the other two characters have equal frequencies. The third case involves all three characters appearing equally often.

For the two-character case, the excluded character acts as a delimiter. We split the string into segments and use a one-dimensional prefix difference to find the longest substring with equal character counts.

For the three-character case, we use a two-dimensional prefix balance state based on the difference between the counts of a and b, and the difference between the counts of b and c. By storing the earliest occurrence of each state using an efficient 64-bit encoding in a hash map, we can identify valid substrings in O(1) average lookup time. This approach achieves O(n) time complexity and O(n) auxiliary space, efficiently handling strings containing up to 100,000 characters.

πŸ”₯ Problem Highlights

βœ… Divided the problem into cases with one, two, and three distinct characters
βœ… Scanned consecutive character runs to solve the one-character case in O(n) time
βœ… Used delimiter-based segment isolation for pairs of distinct characters
βœ… Applied a two-dimensional prefix difference state for balancing three characters
βœ… Used efficient 64-bit bitwise state encoding to avoid string key allocations
βœ… Achieved O(n) time complexity and O(n) auxiliary space

⏱️ TIMESTAMPS

00:00 Intro
01:01 Problem Understanding and Constraints
02:01 Three Disjoint Categories and State Formulation
03:58 Three-Case Linear-Time Algorithmic Strategy
05:28 Java Code Walkthrough and Bit Packing
07:00 Execution and Test Cases Walkthrough
08:11 Time Complexity Analysis O(n)
09:05 Space Complexity Analysis O(n)
09:39 Outro and Wrap-up

πŸ“‚ Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/13-February/3714-Longest-Balanced-Substring-II

β˜• Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/13-February/3714-Longest-Balanced-Substring-II/Solution.java

🐍 Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/13-February/3714-Longest-Balanced-Substring-II/solution.py

⚑ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/13-February/3714-Longest-Balanced-Substring-II/solution.cpp

🌐 JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/13-February/3714-Longest-Balanced-Substring-II/solution.js

πŸ”— LeetCode Source Code

πŸ“Œ Add the link here

🌐 All LeetCode Solutions

πŸ“Œ https://codebytushu.com/Leetcode/

πŸ“± Join CodeByTushu on WhatsApp

Get coding updates, LeetCode solutions, daily questions and more.

https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40

❀️ Donate

πŸ“Œ https://codebytushu.com/Leetcode/donate.php

πŸ”₯ Perfect For

LeetCode Medium Problems
Java DSA Preparation
Prefix Sum State Encoding
Multi-Character Balance Invariants
Linear Time Optimization
Coding Interview Preparation
Problem Solving

🌐 Follow CodeByTushu

▢️ YouTube: https://www.youtube.com/@codebytushu

πŸ“Έ Instagram: https://instagram.com/codebytushu/

πŸ’Ό LinkedIn: https://www.linkedin.com/company/codebytushu/

🌍 Website: https://codebytushu.com

πŸ™ GitHub: https://github.com/CodeByTushu

❀️ Don’t Forget To

πŸ‘ Like | πŸ’¬ Comment | πŸ”” Subscribe

πŸ’¬ Question of the Day

Why can equal frequencies of a, b, and c be identified using a two-dimensional state containing the difference between the counts of a and b, and the difference between the counts of b and c?

1️⃣ Because both differences become zero when all three character counts are equal
2️⃣ Because prefix sums only work when the difference is strictly positive
3️⃣ Because the string length must always be a multiple of three
4️⃣ Because the character c always appears at least once in every substring

#LeetCode3714 #LeetCode #LongestBalancedSubstringII #Java #JavaDSA #String #PrefixSum #HashTable #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu

πŸ‘‡ Comment the answer below! πŸ”₯Β Close description
Β Β Β Β Β Β 
Videos related to "LeetCode 3714 | Longest Balanced Substring II | Java | Medium | Prefix Sum | CodeByTushu"