본문 바로가기

Tech/[PS] Reviews

[프로그래머스-코딩 기초 트레이닝] 접미사인지 확인하기

Hi, There!
안녕하세요, 바오밥입니다.


목차

  • 문제
  • 풀이

 


문제

문제 내용

https://school.programmers.co.kr/learn/courses/30/lessons/181908

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


풀이

나의 풀이

class Solution {
    public int solution(String my_string, String is_suffix) {
        int answer = 0;
        String[] suffix = new String[my_string.length()];
        
        int idx = 0;
        for(int i=my_string.length()-1; i>=0; i--) {
            suffix[idx] = my_string.substring(i, my_string.length());
            idx++;
        }
        
        for(String str : suffix) {
            if(is_suffix.equals(str)) answer = 1;
        }
        
        return answer;
    }
}

 

다른 사람의 풀이

class Solution {
    public int solution(String my_string, String is_suffix) {
        return my_string.endsWith(is_suffix) ? 1 : 0; 
        // String.endsWith(String str); str이 접미사인지 아닌지 판별하여 true, false 반환
    }
}