Problem:
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 be isBeautifulString(inputString) = true.
This string contains 3 as, 3 bs, 1 c, 1 d, 1 e, and 1 f (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 be isBeautifulString(inputString) = false.
Since there are more bs than as, this string is not beautiful.
-
For inputString = "bbc", the output should be isBeautifulString(inputString) = false.
Although there are more bs than cs, this string is not beautiful because there are no as, so therefore there are more bs than as.
-Summary-
1. Create a temp_dict to count and save all alphabet characters.
2. Initialize with all 0.
3. count each character in inputString and update on the temp_dict.
4. Using for loop, we compare the alphabet of previous and current, if previous is smaller than current, it will return False.
5. After loop, return True.
문제를 약간 brute force처럼 풀어서 코드가 너무 정리없이 되어있는것 같다. 다른 사람들이 푼 것을 보니, 파이썬의 string.ascii_lowercase() 함수와 ascii code 를 이용하면 좀 더 깔끔하게 풀수 있다.
def isBeautifulString(inputString):
prev = 50
for i in string.ascii_lowercase:
letter = inputString.count(i)
if letter>prev:
return False
prev = letter
return True
모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]
'CodeSignal > Arcade' 카테고리의 다른 글
CodeSignal [45/60] buildPalindrome (0) | 2020.05.27 |
---|---|
CodeSignal [44/60] findEmailDomain (0) | 2020.05.26 |
CodeSignal [42/60] bishopAndPawn (0) | 2020.05.26 |
CodeSignal [41/60] digitDegree (0) | 2020.05.26 |
CodeSignal [40/60] longestDigitsPrefix (0) | 2020.05.26 |
댓글