FastPrepLongest Equal-Frequency Substring

Longest Equal-Frequency Substring

DRW logoDRW● MediumINTERNPHONE SCREEN
Learn

Problem statement

You are given a lowercase English string s. A substring is balanced when every distinct letter that appears in it has the same frequency.

Return the maximum length of a balanced substring.

Function

longestBalancedSubstring(s: String) → int

Examples

Example 1

s = "abbac"return = 4

The substring abba contains two as and two bs.

Example 2

s = "aaaa"return = 4

A substring containing only one distinct letter is balanced.

Example 3

s = "abcabc"return = 6

Each of a, b, and c occurs twice.

Constraints

  • 1 <= s.length <= 1000
  • s contains only lowercase English letters.

More DRW problems

See DRW hiring insights
public int longestBalancedSubstring(String s) {
    // Write your code here.
}
s"abbac"
expected4
Checking account…