문제.

Untitled

제한 사항

입출력 예

my_string m c result
"ihrhbakrfpndopljhygc" 4 2 "happy"
"programmers" 1 1 "programmers"

코드

- 내 코드
public class Solution181904 {
  public static StringBuilder solution(String my_string, int m, int c) {
    // round
    int round = my_string.length() / m;
    // 2차원 배열
    Character[][] character = new Character[m][round]; // [4][5]
    // StringBuilder
    StringBuilder result = new StringBuilder();

    // 이중 반복문 - 이차원 배열 값 넣기
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < round; j++) {
        character[i][j] = my_string.charAt(m * j + i);
      }
    }

    // 합치기
    for (int i = 0; i < character[c -1].length; i++) {
      result.append(character[c -1][i]);
    }

    return result;
  }

  public static void main(String[] args) {
    String my_string = "ihrhbakrfpndopljhygc";
    int m = 4;
    int c = 2;
    System.out.println(solution(my_string, m, c));
  }
}
- 연구 코드
class Solution {

    public String solution(String my_string, int m, int c) {
        String answer = "";

        for (int i = c - 1; i < my_string.length(); i += m) {
            answer += my_string.charAt(i);
        }
        return answer;
    }
}

문제를 너무 어렵게 풀었다. 나는 2차원 배열로 풀었지만 이렇게 간단하게 풀 수 있는 방법이 존재했다.