[Programmers] Lv2. λͺ¨μŒ 사전 | C++

2023. 6. 4. 19:34ㆍCoding Test/Programmers

πŸ”—λ¬Έμ œ λ³΄λŸ¬κ°€κΈ°
 

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€

μ½”λ“œ μ€‘μ‹¬μ˜ 개발자 μ±„μš©. μŠ€νƒ 기반의 ν¬μ§€μ…˜ 맀칭. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 개발자 λ§žμΆ€ν˜• ν”„λ‘œν•„μ„ λ“±λ‘ν•˜κ³ , λ‚˜μ™€ 기술 ꢁ합이 잘 λ§žλŠ” 기업듀을 맀칭 λ°›μœΌμ„Έμš”.

programmers.co.kr

 

πŸ‘¨‍πŸ’»ν’€μ΄ κ³Όμ •

 

λ°±νŠΈλž˜ν‚Ή(Backtracking)을 μ΄μš©ν•΄ 사전 μˆœμ„œλŒ€λ‘œ 단어λ₯Ό μ‘°ν•©ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€. 1)μ•ŒνŒŒλ²³μ„ ν•˜λ‚˜μ”© 뢙이고 λ–ΌλŠ” κ³Όμ •μ—μ„œ 인덱슀 번호λ₯Ό μ¦κ°€μ‹œν‚€κ³ , 2)μ›ν•˜λŠ” 단어λ₯Ό 찾으면 탐색을 μ€‘μ§€ν•˜λ„λ‘ κ΅¬ν˜„ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μž¬κ·€(Recursion)와 λ°±νŠΈλž˜ν‚Ή(Backtracking) κ°œλ…μ΄ μ²˜μŒμ—λŠ” μ΅μˆ™ν•˜μ§€ μ•Šμ•„, μ½”λ“œλ‘œ κ΅¬ν˜„ν•˜κΈ° μ–΄λ €μ› λŠ”λ° 점점 μ—°μŠ΅ν•˜λ‹€ λ³΄λ‹ˆ μ΅μˆ™ν•΄ μ§€λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

 

βœοΈμ†ŒμŠ€ μ½”λ“œ 및 κ²°κ³Ό

#include <string>
#include <vector>
using namespace std;
const int VOWEL_NUM = 5;
const char vowel[VOWEL_NUM] = { 'A', 'E', 'I', 'O', 'U' };
bool isFindingWord = false;

void CreateCombinationWords(const string& word, string targetWord, int& index)
{
    if (targetWord.length() == VOWEL_NUM)
        return;

    for (int i = 0; i < VOWEL_NUM; i++)
    {
        targetWord.push_back(vowel[i]);
        index++;
        if (targetWord.compare(word) == 0)
        {
            isFindingWord = true;
            break;
        }

        CreateCombinationWords(word, targetWord, index);
        if (isFindingWord)
            break;

        targetWord.pop_back();
    }
}

int GetWordIndex(const string& word)
{
    int index = 0;
    string targetWord = "";
    CreateCombinationWords(word, targetWord, index);
    return index;
}

int solution(string word) {
    return GetWordIndex(word);
}

 

제좜 결과

 

 

 

728x90
λ°˜μ‘ν˜•