LeetCode 3713 | Longest Balanced Substring I | Java | Medium | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 3713 β Longest Balanced Substring I using String, Hash Table, and Counting in Java.
We are given a string named s containing lowercase English letters. We need to find the maximum length of a substring where every distinct character appears the same number of times. A brute-force approach would check character frequencies across the alphabet for every possible substring. However, using a mathematical property of frequency sums, all distinct characters have equal frequencies if and only if the total substring length equals the number of distinct characters multiplied by the maximum character frequency.
This useful relationship allows us to validate each candidate window in O(1) time without scanning the entire alphabet. Since the string length is at most 1000, we enumerate each starting position i and expand the ending position j while maintaining a frequency table of size 26. We also apply early pruning whenever the remaining suffix cannot exceed our current maximum length. This gives us O(n^2) time complexity and O(1) auxiliary space.
π₯ Problem Highlights
β Defined the substring frequency balance condition
β Used the mathematical invariant: length equals distinct multiplied by maxFreq
β Enabled O(1) window validation without scanning the alphabet
β Applied early-exit pruning when the remaining characters cannot exceed maxLen
β Achieved efficient O(n^2) runtime
β Used O(1) auxiliary space with a fixed-size frequency array of 26 elements
β±οΈ TIMESTAMPS
00:00 Intro
01:02 Problem Understanding and Constraints
02:08 Mathematical Frequency Equality Insight
03:30 Dynamic Character Tracking Strategy
04:55 Java Code Walkthrough
06:20 Execution and Test Cases
07:35 Time Complexity Analysis O(n^2)
08:30 Space Complexity Analysis O(1)
09:08 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I/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
String Window Algorithms
Frequency Table Optimization
Subarray and Substring Invariants
Time Complexity 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 does the condition that length equals distinct multiplied by maxFreq guarantee that all distinct characters in the substring have the same frequency?
1οΈβ£ Because the string only contains lowercase English letters
2οΈβ£ Because every character frequency is at most maxFreq, so the total equals distinct multiplied by maxFreq only when every frequency equals maxFreq
3οΈβ£ Because the distinct character count cannot exceed 26
4οΈβ£ Because every substring with equal frequencies must be a palindrome
#LeetCode3713 #LeetCode #LongestBalancedSubstringI #Java #JavaDSA #String #HashTable #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 3713 β Longest Balanced Substring I using String, Hash Table, and Counting in Java.
We are given a string named s containing lowercase English letters. We need to find the maximum length of a substring where every distinct character appears the same number of times. A brute-force approach would check character frequencies across the alphabet for every possible substring. However, using a mathematical property of frequency sums, all distinct characters have equal frequencies if and only if the total substring length equals the number of distinct characters multiplied by the maximum character frequency.
This useful relationship allows us to validate each candidate window in O(1) time without scanning the entire alphabet. Since the string length is at most 1000, we enumerate each starting position i and expand the ending position j while maintaining a frequency table of size 26. We also apply early pruning whenever the remaining suffix cannot exceed our current maximum length. This gives us O(n^2) time complexity and O(1) auxiliary space.
π₯ Problem Highlights
β Defined the substring frequency balance condition
β Used the mathematical invariant: length equals distinct multiplied by maxFreq
β Enabled O(1) window validation without scanning the alphabet
β Applied early-exit pruning when the remaining characters cannot exceed maxLen
β Achieved efficient O(n^2) runtime
β Used O(1) auxiliary space with a fixed-size frequency array of 26 elements
β±οΈ TIMESTAMPS
00:00 Intro
01:02 Problem Understanding and Constraints
02:08 Mathematical Frequency Equality Insight
03:30 Dynamic Character Tracking Strategy
04:55 Java Code Walkthrough
06:20 Execution and Test Cases
07:35 Time Complexity Analysis O(n^2)
08:30 Space Complexity Analysis O(1)
09:08 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/12-February/3713-Longest-Balanced-Substring-I/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
String Window Algorithms
Frequency Table Optimization
Subarray and Substring Invariants
Time Complexity 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 does the condition that length equals distinct multiplied by maxFreq guarantee that all distinct characters in the substring have the same frequency?
1οΈβ£ Because the string only contains lowercase English letters
2οΈβ£ Because every character frequency is at most maxFreq, so the total equals distinct multiplied by maxFreq only when every frequency equals maxFreq
3οΈβ£ Because the distinct character count cannot exceed 26
4οΈβ£ Because every substring with equal frequencies must be a palindrome
#LeetCode3713 #LeetCode #LongestBalancedSubstringI #Java #JavaDSA #String #HashTable #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
