문제 링크 : 모음사전
알파벳의 개수가 5개 밖에 되지 않기 때문에 전부 탐색해도 시간 안에 해결 가능한 문제이다.
따라서 문자 최대 크기가 n개일 때(1<=n<=5) 나올 수 있는 모든 경우를 arr에 넣어주고 arr을 정렬해주면 몇 번째 단어인지 쉽게 구할 수 있다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> arr;
string temp[5] = {"A", "E", "I", "O", "U"};
void go(int maxlen, string s) {
if(maxlen == s.size()) {
arr.push_back(s);
return;
}
for(int i=0 ; i<5 ; i++) {
go(maxlen, s+temp[i]);
}
}
int solution(string word) {
int answer = 0;
for(int i=1 ; i<=5 ; i++) {
go(i, "");
}
sort(arr.begin(), arr.end());
answer = find(arr.begin(), arr.end(), word)-arr.begin()+1;
return answer;
}