문제
문자열 s에 나타나는 문자를 큰것부터 작은 순으로 정렬해 새로운 문자열을 리턴하는 함수, solution을 완성해주세요.
s는 영문 대소문자로만 구성되어 있으며, 대문자는 소문자보다 작은 것으로 간주합니다.
제한 사항
- str은 길이 1 이상인 문자열입니다.
코드
#include <string>
#include <algorithm>
using namespace std;
bool cmp(const char& a, const char& b)
{
return a > b;
}
string solution(string s)
{
sort(s.begin(), s.end(), cmp);
return s;
}
나의 생각
아스키코드 상으로 값이 큰 알파벳을 정렬하는 문제다.
위 코드처럼 compare 함수를 정의해도 되고 아래 코드처럼 greater 구조체를 사용해도 된다.
#include <string>
#include <algorithm>
using namespace std;
string solution(string s)
{
sort (s.begin(), s.end(), greater<char>());
return s;
}
'Algorithm > 프로그래머스 : Level 1' 카테고리의 다른 글
[프로그래머스 Level 1] 문자열 다루기 기본 (0) | 2021.11.19 |
---|---|
[프로그래머스 Level 1] 평균 구하기 (0) | 2021.11.19 |
[프로그래머스 Level 1] 제일 작은 수 제거하기 (0) | 2021.11.18 |
[프로그래머스 Level 1] 문자열 내 p와 y의 개수 (0) | 2021.11.17 |
[프로그래머스 Level 1] 정수 제곱근 판별 (0) | 2021.11.17 |