Medium

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.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string of lowercase English letters.

    Guaranteed constraints:
    3 ≤ inputString.length ≤ 50.

  • [output] boolean

    • Return true if the string is beautiful, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool isBeautifulString(char *inputString)
{
	int alphabet[26]={0, };

	for(int i=0;i<strlen(inputString);i++)
		alphabet[inputString[i]-'a']++;
	for(int i=1;i<26;i++)
		if(alphabet[i-1]<alphabet[i])
			return false;

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Elections Winners  (0) 2020.04.06
<Codesignal> buildPalindrome  (0) 2020.04.05
<Codesignal> Bishop and Pawn  (0) 2020.04.05
<Codesignal> digitDegree  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05

+ Recent posts