Contents
문제 풀기
문제 풀기
class Solution {
public String solution(String my_string, int k) {
String answer = my_string.repeat(k);
return answer;
}
}
repeat()
repeat()
은 문자열을 지정한 횟수만큼 반복해서 이어붙인 새 문자열을 반환하는 Java 11+ 메서드쉽게 말해, 문자열의 “곱하기” 기능
📌 기본 문법
String result = original.repeat(count);
original
: 반복할 문자열
count
: 반복 횟수 (0 이상)
- 반환값 : 원본 문자열을
count
번 이어붙인 새 문자열
📌 동작 예시
java
복사편집
System.out.println("ha".repeat(3)); // "hahaha"
System.out.println("*".repeat(5)); // "*****"
System.out.println("abc".repeat(0)); // "" (빈 문자열)
📌 주의사항
- Java 11 이상에서만 사용 가능 (Java 8이나 그 이하 버전에선 없음).
count
가 음수면:
"abc".repeat(-1); // IllegalArgumentException 발생
- 원본 문자열이 빈 문자열이라면, 반복해도 빈 문자열:
"".repeat(10); // ""
- 시간복잡도는 O(문자열 길이 × count) → count가 크면 결과 문자열 길이도 커짐.
Share article