Skip to content

Commit 220e960

Browse files
authored
Added tasks 3163, 3164, 3165
1 parent e2a636c commit 220e960

File tree

4 files changed

+425
-105
lines changed
  • src/main/kotlin/g3101_3200
    • s3163_string_compression_iii
    • s3164_find_the_number_of_good_pairs_ii
    • s3165_maximum_sum_of_subsequence_with_non_adjacent_elements

4 files changed

+425
-105
lines changed

README.md

Lines changed: 108 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
33
> ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews)
44

5-
* [Graph Theory I](#graph-theory-i)
65
* [SQL I](#sql-i)
76
* [Level 1](#level-1)
87
* [Level 2](#level-2)
@@ -16,110 +15,7 @@
1615
* [Dynamic Programming I](#dynamic-programming-i)
1716
* [Programming Skills I](#programming-skills-i)
1817
* [Programming Skills II](#programming-skills-ii)
19-
20-
### Graph Theory I
21-
22-
#### Day 1 Matrix Related Problems
23-
24-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
25-
|-|-|-|-|-|-
26-
| 0733 |[Flood Fill](src/main/kotlin/g0701_0800/s0733_flood_fill)| Easy | Array, Depth_First_Search, Breadth_First_Search, Matrix | 230 | 97.76
27-
| 0200 |[Number of Islands](src/main/kotlin/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 252 | 95.41
28-
29-
#### Day 2 Matrix Related Problems
30-
31-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
32-
|-|-|-|-|-|-
33-
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 181 | 93.83
34-
| 1254 |[Number of Closed Islands](src/main/kotlin/g1201_1300/s1254_number_of_closed_islands)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 177 | 89.47
35-
36-
#### Day 3 Matrix Related Problems
37-
38-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
39-
|-|-|-|-|-|-
40-
| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 283 | 90.70
41-
| 1905 |[Count Sub Islands](src/main/kotlin/g1901_2000/s1905_count_sub_islands)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 866 | 100.00
42-
43-
#### Day 4 Matrix Related Problems
44-
45-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
46-
|-|-|-|-|-|-
47-
| 1162 |[As Far from Land as Possible](src/main/kotlin/g1101_1200/s1162_as_far_from_land_as_possible)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 362 | 81.25
48-
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 319 | 100.00
49-
50-
#### Day 5 Matrix Related Problems
51-
52-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
53-
|-|-|-|-|-|-
54-
| 1091 |[Shortest Path in Binary Matrix](src/main/kotlin/g1001_1100/s1091_shortest_path_in_binary_matrix)| Medium | Array, Breadth_First_Search, Matrix | 305 | 98.28
55-
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 441 | 94.06
56-
57-
#### Day 6 Matrix Related Problems
58-
59-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
60-
|-|-|-|-|-|-
61-
| 0934 |[Shortest Bridge](src/main/kotlin/g0901_1000/s0934_shortest_bridge)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 301 | 80.95
62-
| 1926 |[Nearest Exit from Entrance in Maze](src/main/kotlin/g1901_2000/s1926_nearest_exit_from_entrance_in_maze)| Medium | Array, Breadth_First_Search, Matrix | 351 | 94.44
63-
64-
#### Day 7 Standard Traversal
65-
66-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
67-
|-|-|-|-|-|-
68-
| 0797 |[All Paths From Source to Target](src/main/kotlin/g0701_0800/s0797_all_paths_from_source_to_target)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Backtracking | 232 | 100.00
69-
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 189 | 69.23
70-
71-
#### Day 8 Standard Traversal
72-
73-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
74-
|-|-|-|-|-|-
75-
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
76-
| 1319 |[Number of Operations to Make Network Connected](src/main/kotlin/g1301_1400/s1319_number_of_operations_to_make_network_connected)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 379 | 83.33
77-
78-
#### Day 9 Standard Traversal
79-
80-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
81-
|-|-|-|-|-|-
82-
| 1376 |[Time Needed to Inform All Employees](src/main/kotlin/g1301_1400/s1376_time_needed_to_inform_all_employees)| Medium | Depth_First_Search, Breadth_First_Search, Tree | 915 | 37.62
83-
| 0802 |[Find Eventual Safe States](src/main/kotlin/g0801_0900/s0802_find_eventual_safe_states)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 511 | 100.00
84-
85-
#### Day 10 Standard Traversal
86-
87-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
88-
|-|-|-|-|-|-
89-
| 1129 |[Shortest Path with Alternating Colors](src/main/kotlin/g1101_1200/s1129_shortest_path_with_alternating_colors)| Medium | Breadth_First_Search, Graph | 208 | 80.00
90-
| 1466 |[Reorder Routes to Make All Paths Lead to the City Zero](src/main/kotlin/g1401_1500/s1466_reorder_routes_to_make_all_paths_lead_to_the_city_zero)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 718 | 100.00
91-
| 0847 |[Shortest Path Visiting All Nodes](src/main/kotlin/g0801_0900/s0847_shortest_path_visiting_all_nodes)| Hard | Dynamic_Programming, Breadth_First_Search, Bit_Manipulation, Graph, Bitmask | 164 | 100.00
92-
93-
#### Day 11 Breadth First Search
94-
95-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
96-
|-|-|-|-|-|-
97-
| 1306 |[Jump Game III](src/main/kotlin/g1301_1400/s1306_jump_game_iii)| Medium | Array, Depth_First_Search, Breadth_First_Search | 291 | 100.00
98-
| 1654 |[Minimum Jumps to Reach Home](src/main/kotlin/g1601_1700/s1654_minimum_jumps_to_reach_home)| Medium | Array, Dynamic_Programming, Breadth_First_Search | 192 | 100.00
99-
| 0365 |[Water and Jug Problem](src/main/kotlin/g0301_0400/s0365_water_and_jug_problem)| Medium | Math, Depth_First_Search, Breadth_First_Search | 130 | 100.00
100-
101-
#### Day 12 Breadth First Search
102-
103-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
104-
|-|-|-|-|-|-
105-
| 0433 |[Minimum Genetic Mutation](src/main/kotlin/g0401_0500/s0433_minimum_genetic_mutation)| Medium | String, Hash_Table, Breadth_First_Search | 204 | 82.08
106-
| 0752 |[Open the Lock](src/main/kotlin/g0701_0800/s0752_open_the_lock)| Medium | Array, String, Hash_Table, Breadth_First_Search | 310 | 100.00
107-
| 0127 |[Word Ladder](src/main/kotlin/g0101_0200/s0127_word_ladder)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 396 | 98.68
108-
109-
#### Day 13 Graph Theory
110-
111-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
112-
|-|-|-|-|-|-
113-
| 0997 |[Find the Town Judge](src/main/kotlin/g0901_1000/s0997_find_the_town_judge)| Easy | Array, Hash_Table, Graph | 475 | 58.62
114-
| 1557 |[Minimum Number of Vertices to Reach All Nodes](src/main/kotlin/g1501_1600/s1557_minimum_number_of_vertices_to_reach_all_nodes)| Medium | Graph | 792 | 99.29
115-
116-
#### Day 14 Graph Theory
117-
118-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
119-
|-|-|-|-|-|-
120-
| 1615 |[Maximal Network Rank](src/main/kotlin/g1601_1700/s1615_maximal_network_rank)| Medium | Graph | 282 | 100.00
121-
| 0886 |[Possible Bipartition](src/main/kotlin/g0801_0900/s0886_possible_bipartition)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 397 | 100.00
122-
| 0785 |[Is Graph Bipartite?](src/main/kotlin/g0701_0800/s0785_is_graph_bipartite)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 215 | 82.35
18+
* [Graph Theory I](#graph-theory-i)
12319

12420
### SQL I
12521

@@ -1812,10 +1708,117 @@
18121708
| 0622 |[Design Circular Queue](src/main/kotlin/g0601_0700/s0622_design_circular_queue)| Medium | Array, Design, Linked_List, Queue | 234 | 92.68
18131709
| 0729 |[My Calendar I](src/main/kotlin/g0701_0800/s0729_my_calendar_i)| Medium | Binary_Search, Design, Ordered_Set, Segment_Tree | 378 | 69.70
18141710

1711+
### Graph Theory I
1712+
1713+
#### Day 1 Matrix Related Problems
1714+
1715+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1716+
|-|-|-|-|-|-
1717+
| 0733 |[Flood Fill](src/main/kotlin/g0701_0800/s0733_flood_fill)| Easy | Array, Depth_First_Search, Breadth_First_Search, Matrix | 230 | 97.76
1718+
| 0200 |[Number of Islands](src/main/kotlin/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 252 | 95.41
1719+
1720+
#### Day 2 Matrix Related Problems
1721+
1722+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1723+
|-|-|-|-|-|-
1724+
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 181 | 93.83
1725+
| 1254 |[Number of Closed Islands](src/main/kotlin/g1201_1300/s1254_number_of_closed_islands)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 177 | 89.47
1726+
1727+
#### Day 3 Matrix Related Problems
1728+
1729+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1730+
|-|-|-|-|-|-
1731+
| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 283 | 90.70
1732+
| 1905 |[Count Sub Islands](src/main/kotlin/g1901_2000/s1905_count_sub_islands)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 866 | 100.00
1733+
1734+
#### Day 4 Matrix Related Problems
1735+
1736+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1737+
|-|-|-|-|-|-
1738+
| 1162 |[As Far from Land as Possible](src/main/kotlin/g1101_1200/s1162_as_far_from_land_as_possible)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 362 | 81.25
1739+
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 319 | 100.00
1740+
1741+
#### Day 5 Matrix Related Problems
1742+
1743+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1744+
|-|-|-|-|-|-
1745+
| 1091 |[Shortest Path in Binary Matrix](src/main/kotlin/g1001_1100/s1091_shortest_path_in_binary_matrix)| Medium | Array, Breadth_First_Search, Matrix | 305 | 98.28
1746+
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 441 | 94.06
1747+
1748+
#### Day 6 Matrix Related Problems
1749+
1750+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1751+
|-|-|-|-|-|-
1752+
| 0934 |[Shortest Bridge](src/main/kotlin/g0901_1000/s0934_shortest_bridge)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 301 | 80.95
1753+
| 1926 |[Nearest Exit from Entrance in Maze](src/main/kotlin/g1901_2000/s1926_nearest_exit_from_entrance_in_maze)| Medium | Array, Breadth_First_Search, Matrix | 351 | 94.44
1754+
1755+
#### Day 7 Standard Traversal
1756+
1757+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1758+
|-|-|-|-|-|-
1759+
| 0797 |[All Paths From Source to Target](src/main/kotlin/g0701_0800/s0797_all_paths_from_source_to_target)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Backtracking | 232 | 100.00
1760+
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 189 | 69.23
1761+
1762+
#### Day 8 Standard Traversal
1763+
1764+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1765+
|-|-|-|-|-|-
1766+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
1767+
| 1319 |[Number of Operations to Make Network Connected](src/main/kotlin/g1301_1400/s1319_number_of_operations_to_make_network_connected)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 379 | 83.33
1768+
1769+
#### Day 9 Standard Traversal
1770+
1771+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1772+
|-|-|-|-|-|-
1773+
| 1376 |[Time Needed to Inform All Employees](src/main/kotlin/g1301_1400/s1376_time_needed_to_inform_all_employees)| Medium | Depth_First_Search, Breadth_First_Search, Tree | 915 | 37.62
1774+
| 0802 |[Find Eventual Safe States](src/main/kotlin/g0801_0900/s0802_find_eventual_safe_states)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 511 | 100.00
1775+
1776+
#### Day 10 Standard Traversal
1777+
1778+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1779+
|-|-|-|-|-|-
1780+
| 1129 |[Shortest Path with Alternating Colors](src/main/kotlin/g1101_1200/s1129_shortest_path_with_alternating_colors)| Medium | Breadth_First_Search, Graph | 208 | 80.00
1781+
| 1466 |[Reorder Routes to Make All Paths Lead to the City Zero](src/main/kotlin/g1401_1500/s1466_reorder_routes_to_make_all_paths_lead_to_the_city_zero)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 718 | 100.00
1782+
| 0847 |[Shortest Path Visiting All Nodes](src/main/kotlin/g0801_0900/s0847_shortest_path_visiting_all_nodes)| Hard | Dynamic_Programming, Breadth_First_Search, Bit_Manipulation, Graph, Bitmask | 164 | 100.00
1783+
1784+
#### Day 11 Breadth First Search
1785+
1786+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1787+
|-|-|-|-|-|-
1788+
| 1306 |[Jump Game III](src/main/kotlin/g1301_1400/s1306_jump_game_iii)| Medium | Array, Depth_First_Search, Breadth_First_Search | 291 | 100.00
1789+
| 1654 |[Minimum Jumps to Reach Home](src/main/kotlin/g1601_1700/s1654_minimum_jumps_to_reach_home)| Medium | Array, Dynamic_Programming, Breadth_First_Search | 192 | 100.00
1790+
| 0365 |[Water and Jug Problem](src/main/kotlin/g0301_0400/s0365_water_and_jug_problem)| Medium | Math, Depth_First_Search, Breadth_First_Search | 130 | 100.00
1791+
1792+
#### Day 12 Breadth First Search
1793+
1794+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1795+
|-|-|-|-|-|-
1796+
| 0433 |[Minimum Genetic Mutation](src/main/kotlin/g0401_0500/s0433_minimum_genetic_mutation)| Medium | String, Hash_Table, Breadth_First_Search | 204 | 82.08
1797+
| 0752 |[Open the Lock](src/main/kotlin/g0701_0800/s0752_open_the_lock)| Medium | Array, String, Hash_Table, Breadth_First_Search | 310 | 100.00
1798+
| 0127 |[Word Ladder](src/main/kotlin/g0101_0200/s0127_word_ladder)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 396 | 98.68
1799+
1800+
#### Day 13 Graph Theory
1801+
1802+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1803+
|-|-|-|-|-|-
1804+
| 0997 |[Find the Town Judge](src/main/kotlin/g0901_1000/s0997_find_the_town_judge)| Easy | Array, Hash_Table, Graph | 475 | 58.62
1805+
| 1557 |[Minimum Number of Vertices to Reach All Nodes](src/main/kotlin/g1501_1600/s1557_minimum_number_of_vertices_to_reach_all_nodes)| Medium | Graph | 792 | 99.29
1806+
1807+
#### Day 14 Graph Theory
1808+
1809+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1810+
|-|-|-|-|-|-
1811+
| 1615 |[Maximal Network Rank](src/main/kotlin/g1601_1700/s1615_maximal_network_rank)| Medium | Graph | 282 | 100.00
1812+
| 0886 |[Possible Bipartition](src/main/kotlin/g0801_0900/s0886_possible_bipartition)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 397 | 100.00
1813+
| 0785 |[Is Graph Bipartite?](src/main/kotlin/g0701_0800/s0785_is_graph_bipartite)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 215 | 82.35
1814+
18151815
## Algorithms
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3165 |[Maximum Sum of Subsequence With Non-adjacent Elements](src/main/kotlin/g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements)| Hard | Array, Dynamic_Programming, Divide_and_Conquer, Segment_Tree | 1301 | 22.22
1820+
| 3164 |[Find the Number of Good Pairs II](src/main/kotlin/g3101_3200/s3164_find_the_number_of_good_pairs_ii)| Medium | Array, Hash_Table | 1175 | 90.00
1821+
| 3163 |[String Compression III](src/main/kotlin/g3101_3200/s3163_string_compression_iii)| Medium | String | 331 | 66.13
18191822
| 3162 |[Find the Number of Good Pairs I](src/main/kotlin/g3101_3200/s3162_find_the_number_of_good_pairs_i)| Easy | Array, Hash_Table | 182 | 54.41
18201823
| 3161 |[Block Placement Queries](src/main/kotlin/g3101_3200/s3161_block_placement_queries)| Hard | Array, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 1701 | 100.00
18211824
| 3160 |[Find the Number of Distinct Colors Among the Balls](src/main/kotlin/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls)| Medium | Array, Hash_Table, Simulation | 1055 | 58.82
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3163\. String Compression III
5+
6+
Medium
7+
8+
Given a string `word`, compress it using the following algorithm:
9+
10+
* Begin with an empty string `comp`. While `word` is **not** empty, use the following operation:
11+
* Remove a maximum length prefix of `word` made of a _single character_ `c` repeating **at most** 9 times.
12+
* Append the length of the prefix followed by `c` to `comp`.
13+
14+
Return the string `comp`.
15+
16+
**Example 1:**
17+
18+
**Input:** word = "abcde"
19+
20+
**Output:** "1a1b1c1d1e"
21+
22+
**Explanation:**
23+
24+
Initially, `comp = ""`. Apply the operation 5 times, choosing `"a"`, `"b"`, `"c"`, `"d"`, and `"e"` as the prefix in each operation.
25+
26+
For each prefix, append `"1"` followed by the character to `comp`.
27+
28+
**Example 2:**
29+
30+
**Input:** word = "aaaaaaaaaaaaaabb"
31+
32+
**Output:** "9a5a2b"
33+
34+
**Explanation:**
35+
36+
Initially, `comp = ""`. Apply the operation 3 times, choosing `"aaaaaaaaa"`, `"aaaaa"`, and `"bb"` as the prefix in each operation.
37+
38+
* For prefix `"aaaaaaaaa"`, append `"9"` followed by `"a"` to `comp`.
39+
* For prefix `"aaaaa"`, append `"5"` followed by `"a"` to `comp`.
40+
* For prefix `"bb"`, append `"2"` followed by `"b"` to `comp`.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= word.length <= 2 * 10<sup>5</sup></code>
45+
* `word` consists only of lowercase English letters.
46+
47+
## Solution
48+
49+
```kotlin
50+
class Solution {
51+
fun compressedString(word: String): String {
52+
val builder = StringBuilder()
53+
var last = word[0]
54+
var count = 1
55+
var i = 1
56+
val l = word.length
57+
while (i < l) {
58+
if (word[i] == last) {
59+
count++
60+
if (count == 10) {
61+
builder.append(9).append(last)
62+
count = 1
63+
}
64+
} else {
65+
builder.append(count).append(last)
66+
last = word[i]
67+
count = 1
68+
}
69+
i++
70+
}
71+
builder.append(count).append(last)
72+
return builder.toString()
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)