Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20210416] Java String array elements remove not contain String #99

Open
JuHyun419 opened this issue Apr 16, 2021 · 0 comments
Open

[20210416] Java String array elements remove not contain String #99

JuHyun419 opened this issue Apr 16, 2021 · 0 comments
Labels

Comments

@JuHyun419
Copy link
Owner

String 배열의 각 요소들이 특정 문자열에서 없는 문자 제거하기(?)

  • String skill = "CBD";
  • String[] skillTrees = {"BACDE", "CBADF", "AECB", "BDA"};
  • skillTrees[] 배열의 요소 중 skill의 문자인 "C", "B", "D"를 제외한 나머지 문자는 모두 제거

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

        // [BACDE, CBADF, AECB, BDA]
        System.out.println(Arrays.toString(skillTrees));

        // skill = "CBD";
        removeNotContainSkills(skill, skillTrees);

        // [BCD, CBD, CB, BD]
        System.out.println(Arrays.toString(skillTrees));


        return answer;
    }

    private static void removeNotContainSkills(String skill, String[] skillTrees) {

        // skill에 포함되지 않는 문자 제거
        for (int i = 0; i < skillTrees.length; i++) {
            StringBuilder sb = new StringBuilder(skillTrees[i]);
            for (int j = 0; j < sb.length(); j++) {
                char ch = sb.charAt(j);
                if (isNotContainSkill(skill, ch)) {
                    sb.deleteCharAt(j);
                    j --;
                }
            }
            skillTrees[i] = sb.toString();
        }
    }

    private static boolean isNotContainSkill(final String str, final char ch) {
        return str.indexOf(ch) == -1;
    }
@JuHyun419 JuHyun419 added the Java label Apr 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant