Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- geometry
- Number Theory
- Math
- simulation
- hash table
- 파이썬
- Stack
- Counting
- 구현
- SQL
- Binary Tree
- sorting
- two pointers
- 자바
- Class
- Tree
- Matrix
- Data Structure
- string
- implement
- java
- array
- dynamic programming
- bit manipulation
- Binary Search
- database
- Method
- 코딩테스트
- 코테
- greedy
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2138. Divide a String Into Groups of Size k 본문
1. Input
1) 문자열 s
2) 정수 k
3) 문자열 fill
2. Output
1) s의 문자를 앞에서부터 k개씩 잘라서 만든 문자열을 담은 배열을 반환
- 마지막에 k개 미만으로 남았다면, fill로 대신 채워 길이가 k인 문자열을 만든다.
3. Constraint
1) 1 <= s.length <= 100
2) s는 영어 소문자로만 이루어져 있다.
3) 1 <= k <= 100
4) fill에는 영어 소문자 1개가 담겨있다.
4. Example
Input: s = "abcdefghij", k = 3, fill = "x" -> Output: ["abc","def","ghi","jxx"]
설명: s를 3개씩 자르면 마지막에 j 하나가 남으므로 x를 2개 붙여 길이가 3인 문자열로 만든 후 배열에 담아 반환한다.
5. Code
1) 첫 코드(2022/07/05)
int index = 0;
List<String> list = new ArrayList<String>();
while(index+k-1<s.length()){
list.add(s.substring(index, index+k));
index += k;
}
if(index!=s.length()){
String p = "";
for(int i=0 ; i<k-(s.length()-index) ; i++)
p += fill;
list.add(s.substring(index, s.length()) + p);
}
return list.toArray(new String[0]);
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2154. Keep Multiplying Found Values by Two (0) | 2023.01.13 |
---|---|
[LeetCode/Easy] 2148. Count Elements With Strictly Smaller and Greater Elements (0) | 2023.01.13 |
[LeetCode/Easy] 2129. Capitalize the Title (0) | 2023.01.13 |
[LeetCode/Easy] 2124. Check if All A's Appears Before All B's (0) | 2023.01.13 |
[LeetCode/Easy] 2119. A Number After a Double Reversal (0) | 2023.01.13 |