Post

[Programmers] #181908 - 접미사인지 확인하기 [Java][C++][Python]

[Programmers] #181908 - 접미사인지 확인하기 [Java][C++][Python]

문제 링크


1. 아이디어

접미사는 끝에서부터 이어진 부분 문자열이므로, my_string의 끝부분이 is_suffix와 같은지 확인하면 된다. is_suffix가 my_string보다 길면 그 길이만큼 끝부분을 잘라 낼 수 없으므로 접미사가 아니다.


2. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
class Solution {
    public int solution(String my_string, String is_suffix) {
        return my_string.endsWith(is_suffix) ? 1 : 0;
    }
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;

int solution(string my_string, string is_suffix) {
    return my_string.ends_with(is_suffix);
}

ends_with는 C++20부터 쓸 수 있다.

1
2
def solution(my_string, is_suffix):
    return int(my_string.endswith(is_suffix))

This post is licensed under CC BY 4.0 by the author.