문제
문자열 myString이 주어집니다. "x"를 기준으로 해당 문자열을 잘라내 배열을 만든 후 사전순으로 정렬한 배열을 return 하는 solution 함수를 완성해 주세요.
단, 빈 문자열은 반환할 배열에 넣지 않습니다.
솔루션
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> solution(string myString) {
vector<string> answer;
string tmp = "";
for(int i = 0; i < myString.size(); i++){
if(myString[i] == 'x'){
if(tmp != ""){
answer.push_back(tmp);
tmp = "";
}
}
else{
tmp += myString[i];
}
}
if(tmp != ""){
answer.push_back(tmp);
}
sort(answer.begin(), answer.end());
return answer;
}
메모
헤맸었는데 반례 테스트케이스 "xxxxax" ["a"]를 추가해서 디버깅 후 해결함.
'프로그래머스코딩테스트연습풀이 > C++' 카테고리의 다른 글
[프로그래머스/C++] 세로 읽기 (0) | 2025.02.22 |
---|---|
[프로그래머스/C++] ad 제거하기 (0) | 2025.02.22 |
[프로그래머스/C++] 문자열로 변환 (0) | 2025.02.22 |
[프로그래머스/C++] 배열 만들기 3 (0) | 2025.02.22 |
[프로그래머스/C++] 숫자 찾기 (0) | 2025.02.22 |