Post

[Programmers] #181907 - 문자열의 앞의 n글자 [Java][C++][Python]

문자열에서 앞의 n글자만 잘라내어 반환하는 워밍업 문제.

[Programmers] #181907 - 문자열의 앞의 n글자 [Java][C++][Python]

문제 링크


1. 아이디어

my_string의 처음부터 n번째 문자까지를 잘라낸 부분 문자열이 곧 답이다.


2. 복잡도

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

($N$ = 잘라낼 길이 n)


3. 코드

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

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

string solution(string my_string, int n) {
    return my_string.substr(0, n);
}
1
2
def solution(my_string, n):
    return my_string[:n]

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