[Programmers] #181908 - 접미사인지 확인하기 [Java][C++][Python]
[Programmers] #181908 - 접미사인지 확인하기 [Java][C++][Python]
1. 아이디어
my_string의 뒤쪽 끝부분이 is_suffix와 그대로 일치하는지만 확인하면 되는 문제다. 문자열이 특정 접미사로 끝나는지 판정하는 표준 함수로 바로 처리할 수 있다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(1)$ |
($N$ = is_suffix의 길이)
3. 코드
풀이 [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);
}
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.