Post

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

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

문제 링크


1. 아이디어

접두사는 문자열의 앞쪽 일부이므로 my_string이 is_prefix로 시작하는지만 보면 된다. is_prefix가 my_string보다 길면 my_string의 앞쪽 일부가 될 수 없어 접두사가 아니다.


2. 코드

풀이 [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);
}

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

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.