문제
내 풀이
cclass Solution {
public String[] solution(String my_str, int n) {
String[] answer = {};
if(my_str.length()%n==0){
answer = new String[my_str.length()/n];
for(int i = 0 ; i < my_str.length()/n ; i++ ){
answer[i] = my_str.substring(n*i,n*i+n);
}
}else{
answer = new String[(my_str.length()/n)+1];
for(int i = 0 ; i < my_str.length()/n ; i++ ){
answer[i] = my_str.substring(n*i,n*i+n);
}
answer[my_str.length()/n] = my_str.substring((my_str.length()/n)*n);
}
return answer;
}
}
다른 사람 풀이
import java.util.*;
class Solution {
public ArrayList solution(String my_str, int n) {
ArrayList<String> answer = new ArrayList<String>();
int num = my_str.length()/n;
if(my_str.length()%n != 0){
num = my_str.length()/n+1;
}
for(int i=0; i<num; i++){
if(i == num-1){
answer.add(my_str.substring(i*n));
}else{
answer.add(my_str.substring(i*n,(i+1)*n));
}
}
return answer;
}
}