[Programmers] #181908 - 접미사인지 확인하기 [Java][C++][Python]
문자열이 다른 문자열의 접미사인지 판별하는 워밍업 문제.
[Programmers] #181908 - 접미사인지 확인하기 [Java][C++][Python]
1. 아이디어
my_string이 is_suffix로 끝나는지만 확인하면 되므로, my_string의 뒤쪽에서 is_suffix와 같은 길이만큼 잘라낸 부분 문자열이 is_suffix와 일치하는지 비교하면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(L)$ | $O(L)$ |
($L$ = 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
7
#include <bits/stdc++.h>
using namespace std;
int solution(string my_string, string is_suffix) {
if (is_suffix.size() > my_string.size()) return 0;
return my_string.substr(my_string.size() - is_suffix.size()) == 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.