[프로그래머스] 접두사인지 확인하기

Seah Lee·2023년 6월 28일
0

프로그래머스

목록 보기
48/57

import java.util.Arrays;

class Solution {
    public int solution(String my_string, String is_prefix) {
        int answer = 0;
        String[] list = new String[my_string.length()];
        
        for(int i=0;i<my_string.length();i++){
            list[i] = my_string.substring(0,i);
        }
        
        if (Arrays.asList(list).contains(is_prefix)) answer=1;     
        return answer;
    }
}

Arrays 클래스 가져와야함 -> asList() : Arrays의 정적 메소드, 매개변수에서 가져온 지정된 배열의 고정 목록을 반환
contains() -> List의 메소드

[다른 사람의 풀이]

class Solution {
    public int solution(String my_string, String is_prefix) {
        if (my_string.startsWith(is_prefix)) return 1;
        return 0;
    }
}

.startsWith()
.endsWith()

profile
성장하는 개발자

0개의 댓글