Task
A string is said to be beautiful if each letter in the string appears at most as many times as the previous letter in the alphabet within the string; ie: b occurs no more times than a; c occurs no more times than b; etc.
Given a string, check whether it is beautiful.
Example
-
For
inputString = "bbbaacdafe"
, the output should beisBeautifulString(inputString) = true
;
This string contains 3a
s, 3b
s, 1c
, 1d
, 1e
, and 1f
(and 0 of every other letter), so since there aren’t any letters that appear more frequently than the previous letter, this string qualifies as beautiful. -
For
inputString = "aabbb"
, the output should beisBeautifulString(inputString) = false
;
Since there are moreb
s thana
s, this string is not beautiful. -
For
inputString = "bbc"
, the output should beisBeautifulString(inputString) = false
.
Although there are moreb
s thanc
s, this string is not beautiful because there are noa
s, so therefore there are moreb
s thana
s.
Input/Output
-
[execution time limit]
4 seconds (py3) -
[input] string inputString
A string of lowercase English letters.
Guaranteed constraints:3 ≤ inputString.length ≤ 50
. -
[output] boolean
Returntrue
if the string is beautiful,false
otherwise.
My Solution
def isBeautifulString(inputString):
abcCount = [0 for i in range(26)]
for letter in inputString:
abcCount[ord(letter) - 97] += 1
for i in range(1, len(abcCount)):
if abcCount[i] > abcCount[i - 1]:
return False
return True