Post

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

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

문제 링크


1. 아이디어

접두사 여부는 my_string의 앞부분이 is_prefix와 그대로 일치하는지 확인하면 판단할 수 있다. 문자열이 특정 접두사로 시작하는지 판정하는 표준 함수로 바로 처리할 수 있다.


2. 복잡도

접근시간공간
풀이$O(N)$$O(1)$

($N$ = is_prefix의 길이)


3. 코드

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

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

int solution(string my_string, string is_prefix) {
    return my_string.starts_with(is_prefix);
}
1
2
def solution(my_string, is_prefix):
    return int(my_string.startswith(is_prefix))

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