
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the longest Substring with At Least n Repeating Characters in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and a positive integer n as the second argument.
The string is likely to contain some repeating characters. The function should find out and return the length of the longest substring from the original string in which all characters appear at least n number of times.
For example −
If the input string and the number are −
const str = 'kdkddj'; const num = 2;
Then the output should be −
const output = 5;
because the desired longest substring is 'kdkdd'
Example
Following is the code −
const str = 'kdkddj'; const num = 2; const longestSubstring = (str = '', num) => { if(str.length < num){ return 0 }; const map = {} for(let char of str) { if(char in map){ map[char] += 1; }else{ map[char] = 1; } } const minChar = Object.keys(map).reduce((minKey, key) => map[key] < map[minKey] ? key : minKey) if(map[minChar] >= num){ return str.length; }; substrings = str.split(minChar).filter((subs) => subs.length >= num); if(substrings.length == 0){ return 0; }; let max = 0; for(ss of substrings) { max = Math.max(max, longestSubstring(ss, num)) }; return max; }; console.log(longestSubstring(str, num));
Output
Following is the console output −
5
- Related Articles
- Longest Substring with At Least K Repeating Characters in C++
- Longest Substring Without Repeating Characters in Python
- Finding longest substring between two same characters JavaScript
- Longest Repeating Substring in C++
- Longest Substring with At Most Two Distinct Characters in C++
- Longest Substring with At Most K Distinct Characters in C++
- Finding the longest substring uncommon in array in JavaScript
- Finding the longest common consecutive substring between two strings in JavaScript
- Finding the length of longest vowel substring in a string using JavaScript
- Program to find length of longest substring with character count of at least k in Python
- How to find the length of the longest substring from the given string without repeating the characters using C#?
- Find the longest substring with k unique characters in a given string in Python
- Longest string with two distinct characters in JavaScript
- Program to find length of longest repeating substring in a string in Python
- Finding first non-repeating character JavaScript

Advertisements