[Programmers] #181907 - 문자열의 앞의 n글자 [Java][C++][Python]
[Programmers] #181907 - 문자열의 앞의 n글자 [Java][C++][Python]
1. 아이디어
앞의 $n$글자는 인덱스 0부터 $n$ 직전까지의 구간이다. $n$이 my_string의 길이 이하로 보장되므로 구간이 문자열 밖으로 나가지 않는다.
2. 코드
풀이 [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.