Is Beautiful String

Description


A string is said to be beautiful if b occurs in it no more times than a; c occurs in it no more times than b; etc.

Given a string, check whether it is beautiful.

Example

  • For inputString = "bbbaacdafe", the output should be isBeautifulString(inputString) = true;
  • For inputString = "aabbb", the output should be isBeautifulString(inputString) = false;
  • For inputString = "bbc", the output should be isBeautifulString(inputString) = false.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] string inputString

A string of lowercase letters.

Guaranteed constraints: 3 ≤ inputString.length ≤ 50.

  • [output] boolean

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
    console.log("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solution


1
2
3
function isBeautifulString(inputString) {
    return inputString.split('').sort().join('').replace(/([a-z])\1*/g,(it)=>it.length +',').split(',').slice(0,-1).map(Number).every((el,i,ar)=> (inputString.indexOf(String.fromCharCode(i+'a'.charCodeAt(0)))>=0 &&(i==0 || ar[i]<=ar[i-1])))
}