|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 443. String Compression |
5 |
| - * |
6 |
| - * Given an array of characters, compress it in-place. |
7 |
| - * The length after compression must always be smaller than or equal to the original array. |
8 |
| - * Every element of the array should be a character (not int) of length 1. |
9 |
| - * After you are done modifying the input array in-place, return the new length of the array. |
10 | 3 |
|
11 |
| - Example 1: |
12 |
| - Input: |
13 |
| - ["a","a","b","b","c","c","c"] |
14 |
| -
|
15 |
| - Output: |
16 |
| - Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"] |
17 |
| -
|
18 |
| - Explanation: |
19 |
| - "aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3". |
20 |
| -
|
21 |
| - Example 2: |
22 |
| - Input: |
23 |
| - ["a"] |
24 |
| -
|
25 |
| - Output: |
26 |
| - Return 1, and the first 1 characters of the input array should be: ["a"] |
27 |
| -
|
28 |
| - Explanation: |
29 |
| - Nothing is replaced. |
30 |
| -
|
31 |
| - Example 3: |
32 |
| - Input: |
33 |
| - ["a","b","b","b","b","b","b","b","b","b","b","b","b"] |
34 |
| -
|
35 |
| - Output: |
36 |
| - Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"]. |
37 |
| -
|
38 |
| - Explanation: |
39 |
| - Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12". |
40 |
| - Notice each digit has it's own entry in the array. |
41 |
| -
|
42 |
| - Note: |
43 |
| - All characters have an ASCII value in [35, 126]. |
44 |
| - 1 <= len(chars) <= 1000. |
45 |
| - */ |
46 | 4 | public class _443 {
|
47 | 5 | public static class Solution1 {
|
48 |
| - /**This is breaking the rules, it's not in-place.*/ |
| 6 | + /** |
| 7 | + * This is breaking the rules, it's not in-place. |
| 8 | + */ |
49 | 9 | public int compress(char[] chars) {
|
50 | 10 | if (chars == null || chars.length == 0) {
|
51 | 11 | return 0;
|
|
0 commit comments