알고리즘/codefights

43>isBeautifulString

Diademata 2017. 8. 11. 00:02
반응형

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.


code>>


bool isBeautifulString(std::string inputString) {

int a[26] = { 0, };

for (int i = 0; i < inputString.size(); i++)

{

a[inputString[i] - 'a']++;

}

for (int i = 'b'; i <= 'z'; i++)

{

if (a[i - 'a'] > a[i - 'a' - 1]) return false;

}

return true;

}



다른 사람 풀이 >>


std::algorithm::is_sorted() 시험 범위가 정렬되었는지 여부

template <class ForwardIterator, class Compare>

bool is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);

first - 초기 위치에 전달 반복자.

last - 최종 위치로 전달 반복자.

comp - 두 개의 인수를 승인하고 부울을 반환하는 이항 함수. default '<' 설정되어 있음


bool isBeautifulString(std::string inputString) {

int o[26]{};

for (const auto& c : inputString) {

o[c - 'a']++;

}

return std::is_sorted(std::rbegin(o), std::rend(o));

}



반응형