💡Problem Solving/Programmers

[프로그래머스] 스킬트리 (Java)

gom20 2021. 10. 18. 16:22

#문제

https://programmers.co.kr/learn/courses/30/lessons/49993

 

코딩테스트 연습 - 스킬트리

 

programmers.co.kr

 

#소스코드

import java.util.*;
class Solution {
     
    public HashMap<Character, Integer> priors;
    public int solution(String skill, String[] skill_trees) {
        int answer = 0;  
        priors = new HashMap<Character, Integer>();
        for(int i = 0; i < skill.length(); i++){
            priors.put(skill.charAt(i), i);
        }
        
        for(String s : skill_trees){
            int prior = 0;
            boolean possible = true;           
            for(int i = 0; i < s.length(); i++){
                char c = s.charAt(i);
                if(priors.containsKey(c)){
                    if(prior == priors.get(c)){
                        prior = priors.get(c) + 1;
                    } else {
                        possible = false;
                        break;
                    }
                }
            }
            if(possible) answer++;
        }
      
        return answer;
    }
}