본문 바로가기
코딩테스트/프로그래머스 Lv.0

[코테/0레벨] ad 제거하기

by moca7 2025. 1. 22.

 

class Solution {
    public String[] solution(String[] strArr) {

        int num = 0;
        
        for(int i=0; i<strArr.length; i++){
            if(!strArr[i].contains("ad")){
                num++;
            }
        }
        
        String[] newArr = new String[num];
        int newIndex = 0;
        
        for(int i=0; i<strArr.length; i++){
            if(!strArr[i].contains("ad")){
                newArr[newIndex++] = strArr[i];
            }
        }
        
        return newArr;
    }
}

 

 

- newArr[i]를 하면 ArrayIndexOutOfBoundsException이 발생한다.

그래서 0부터 시작하는 새로운 인덱스를 주고 증감연산자를 사용했다.