이번에는 substring 함수를 써도 괜찮을 것 같습니다!
class Solution {
public int solution(String my_string, String is_suffix) {
int answer = 0;
for(int i = 0; i<my_string.length(); i++){
if(my_string.substring(i).equals(is_suffix)){
answer = 1;
break;
}
}
return answer;
}
}
접미사는 apple이라면 apple, pple, ple, le, e가 접미사이기 때문에 시작점에 i만 넣어주면 됩니다!
하지만 또 다른 방법이 있습니다! 바로 endsWith 함수를 쓰는 것 입ㄴ다.
endsWith은 특정 문자열로 끝나는지 확인해주는 함수입니다.
apple에서도 ap는 false, ple은 true입니다! 따라서 접미사인지 여부도 endsWith으로 하면
class Solution {
public int solution(String my_string, String is_suffix) {
int answer = (my_string.endsWith(is_suffix)) ? 1 : 0;
return answer;
}
}
for문을 돌리지 않아도 되기 때문에 코드가 1줄로 바뀝니다!
그럼 20000~ (。・∀・)ノ゙
'코딩테스트' 카테고리의 다른 글
프로그래머스 - 12세 이하인 여자 환자 목록 출력하기 (0) | 2023.12.19 |
---|---|
프로그래머스 - 접두사인지 확인하기 (0) | 2023.12.18 |
프로그래머스 - 부분 문자열 (1) | 2023.12.18 |
프로그래머스 - 중앙값 구하기 (0) | 2023.10.05 |
프로그래머스 - 배열 두 배 만들기 (0) | 2023.08.24 |