프로그래머스 : ad 제거하기

Digeut·2024년 6월 17일
0

프로그래머스

목록 보기
170/171

❔문제설명

🤔아이디어

contains를 사용해서 ad를 포함하지 않는것만 저장하면 되지 않을까

💡코드풀이

class Solution {
    public String[] solution(String[] strArr) {
        ArrayList<String> list = new ArrayList<>();
        
        for(String str : strArr){
            if (!str.contains("ad")) {
                list.add(str);
            }
        }
        
        String[] answer =list.toArray(new String[list.size()]);
        return answer;
    }
}

ArrayList를 사용한 이유 : 배열의 경우에는 배열크기를 동적으로 변경할수 없다. ArrayList의 경우 동적으로 배열크기를 지정할수 있으므로 바로바로 ad를 포함하지 않는경우를 list에 저장한 후에 array로 변형했다.

profile
백엔드 개발자 지망생

0개의 댓글