본문 바로가기
알고리즘 & 자료구조/프로그래머스

스킬트리

by 신재권 2023. 2. 3.
package programmers;

public class 스킬트리 {

   public static int solution(String skill, String[] skill_trees) {
      int answer = 0;

      if (skill.length() == 1) {
         return skill_trees.length;
      }

      for (int i = 0; i < skill_trees.length; i++) {
         String tmp = skill_trees[i].replaceAll("[^" + skill + " ]", "");

         if (tmp.equals("")) { //선행 스킬 조건에 속해있지 않다면 문자열은 ""만 남아있다.
            answer++;
            continue;
         }

         for (int j = 1; j <= skill.length(); j++) {
            if (skill.substring(0, j).equals(tmp)) {
               answer++;
            }
         }
      }

      return answer;
   }

   public static void main(String[] args) {
      System.out.println(solution("CBD", new String[] {"BACDE", "CBADF", "AECB", "BDA"}));
      System.out.println(solution("CBD", new String[] {"BACDE", "CBADF", "AECB", "BDA", "AQWER"}));
   }

}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

할인 행사  (0) 2023.02.06
땅따먹기  (0) 2023.02.04
주식가격  (0) 2023.02.02
주차 요금 계산  (0) 2023.02.01
n 진수 게임  (1) 2023.01.31