❓ 문제 ❓
위클리 5주차
💯 풀이 방법 💯
완전 탐색 알고리즘으로 풀었다.
cnt변수를 전역변수로 지정하여 단어를 만들때 cnt++을 해주고, 단어가 5자리 수를 넘어가면 cnt--해서 리턴한다.
#include <string>
#include <vector>
using namespace std;
string alpha = "AEIOU";
int answer = 0;
bool val = false;
int cnt = 0;
void dfs(string cur, string word) {
if (cur.size() > 5) {
cnt -= 1;
return;
}
if (cur == word) {
answer = cnt;
val = true;
return;
}
for (int i = 0; i < alpha.size(); i++) {
cnt += 1;
dfs(cur + alpha[i], word);
if (val)
return;
}
}
int solution(string word) {
dfs("", word);
return answer;
}