자료구조 & 알고리즘/프로그래머스
Level 2. 모음사전
diesuki4
2023. 6. 11. 07:42
Level 2. 모음사전
#include <iostream>
using namespace std;
bool rDFS(string s, string& word, int& answer)
{
const static string alphabets[] = {"A", "E", "I", "O", "U"};
++answer;
if (s == word)
return true;
else if (s.length() == 5)
return false;
for (const string& alpha : alphabets)
if (rDFS(s + alpha, word, answer))
return true;
return false;
}
int solution(string word)
{
int answer = 0;
const string alphabets[] = {"A", "E", "I", "O", "U"};
for (const string& alpha : alphabets)
if (rDFS(alpha, word, answer))
return answer;
}
뒤에서부터 개수를 직접 세어 더하는 방법인 것 같다.
#include <iostream>
using namespace std;
int solution(string word)
{
string s("AEIOU");
int n = word.size(), answer(n);
for (int i = 0, b = 1; i < n; ++i, b *= 5)
answer += s.rfind(word[i]) * 781 / b;
return answer;
}