Skip to content

Commit d7fde37

Browse files
solves #2138: Divide a String Into Groups of Size k in java
1 parent 61be039 commit d7fde37

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@
706706
| 2124 | [Check if All A's Appears Before All B's](https://leetcode.com/problems/check-if-all-as-appears-before-all-bs) | [![Java](assets/java.png)](src/CheckIfAllTheAsAppearBeforeAllTheBs.java) | |
707707
| 2129 | [Capitalize the Title](https://leetcode.com/problems/capitalize-the-title) | [![Java](assets/java.png)](src/CapitalizeTheTitle.java) | |
708708
| 2133 | [Check if Every Row and Column Contains All Numbers](https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers) | [![Java](assets/java.png)](src/CheckIfEveryRowAndEveryColumnContainAllNumbers.java) | |
709-
| 2138 | [Divide a String Into Groups of Size k](https://leetcode.com/problems/divide-a-string-into-groups-of-size-k) | | |
709+
| 2138 | [Divide a String Into Groups of Size k](https://leetcode.com/problems/divide-a-string-into-groups-of-size-k) | [![Java](assets/java.png)](src/DivideTheStringIntoGroupsOfSizeK.java) | |
710710
| 2144 | [Minimum Cost of Buying Candies With Discount](https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount) | | |
711711
| 2148 | [Count Elements With Strictly Smaller and Greater Elements](https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements) | | |
712712
| 2154 | [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two) | | |

src/DesignBrowserHistory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
14
class BrowserHistory {
25
private List<String> history;
36
private int ptr;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://leetcode.com/problems/divide-a-string-into-groups-of-size-k
2+
// T: O(s)
3+
// S: O(s)
4+
5+
public class DivideTheStringIntoGroupsOfSizeK {
6+
public String[] divideString(String s, int k, char fill) {
7+
final String filler = fill + "";
8+
final int groups = (int) Math.ceil((double) s.length() / k);
9+
final String[] result = new String[groups];
10+
11+
for (int i = 0 ; i < groups ; i++) {
12+
if (i == result.length - 1) {
13+
if (s.length() - k * i < k) result[i] = s.substring(k * i) + filler.repeat(k - s.length() + k * i);
14+
else result[i] = s.substring(k * i);
15+
} else {
16+
result[i] = s.substring(k * i, k * i + k);
17+
}
18+
}
19+
20+
return result;
21+
}
22+
}

0 commit comments

Comments
 (0)