GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #395 - Longest Substring with At Least K Repeating Characters - LeetCode JavaScript Solutions

Back to all solutions

#395 - Longest Substring with At Least K Repeating Characters

Medium • View on LeetCode • View on GitHub
String Hash Table Sliding Window Divide and Conquer

Problem Description

Given a string s and an integer k, return the length of the longest substring of s such that the frequency of each character in this substring is greater than or equal to k.

If no such substring exists, return 0.

Solution

/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var longestSubstring = function(s, k) {
  for (const char of Array.from(new Set(s))) {
    if (s.match(new RegExp(char, 'g')).length < k) {
      return Math.max(...s.split(char).map(str => longestSubstring(str, k)));
    }
  }
  return s.length;
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.