
- 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 power of a string from a string with repeated letters in JavaScript
The power of the string is the maximum length of a non−empty substring that contains only one unique character.
We are required to write a JavaScript function that takes in a string and returns its power.
For example −
const str = "abbcccddddeeeeedcba"
Then the output should be 5,
because the substring "eeeee" is of length 5 with the character 'e' only.
Example
The code for this will be −
const str = "abbcccddddeeeeedcba" const maxPower = (str = '') => { let power = 1 const sz = str.length - 1 for(let i = 0; i < sz; ++i) { let count = 1 while(i < sz && str[i + 1] === str[i] && ++i) power = Math.max(power, ++count) } return power }; console.log(maxPower(str));
Output
And the output in the console will be −
5
- Related Articles
- How to get the maximum count of repeated letters in a string? JavaScript
- Is the string a combination of repeated substrings in JavaScript
- Finding mistakes in a string - JavaScript
- Finding the ASCII score of a string - JavaScript
- Finding the number of words in a string JavaScript
- Finding number of spaces in a string JavaScript
- Interchanging first letters of words in a string in JavaScript
- Finding duplicate "words" in a string - JavaScript
- Finding missing letter in a string - JavaScript
- Finding the longest word in a string in JavaScript
- Finding shortest word in a string in JavaScript
- Finding hamming distance in a string in JavaScript
- How to return a passed string with letters in alphabetical order in JavaScript?
- Finding the first non-repeating character of a string in JavaScript
- Finding second smallest word in a string - JavaScript

Advertisements