Leetcode
2020.04.25 06:41

443. String Compression

조회 수 94 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.

 

Follow up:
Could you solve it using only O(1) extra space?

 

Example 1:

Input:
["a","a","b","b","c","c","c"]

Output:
Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]

Explanation:
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".

 

Example 2:

Input:
["a"]

Output:
Return 1, and the first 1 characters of the input array should be: ["a"]

Explanation:
Nothing is replaced.

 

Example 3:

Input:
["a","b","b","b","b","b","b","b","b","b","b","b","b"]

Output:
Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].

Explanation:
Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
Notice each digit has it's own entry in the array.

 

Note:

  1. All characters have an ASCII value in [35, 126].
  2. 1 <= len(chars) <= 1000.


class Solution {
    public int compress(char[] chars) {
        List<Character> list = new ArrayList<>();
        char curr = chars[0];
        int count = 1;
         
        for(int i=0; i<chars.length; i++){
            int next = i + 1;
            if(next < chars.length && chars[i] == chars[next]){
                count++;
            }else{
                list.add(curr);
                curr = chars[next<chars.length?next:i];
                if(count > 1){
                    for(int j=0; j<(count+"").length(); j++){
                        list.add((count+"").charAt(j));
                    }
                }
                count = 1;
            }
        }
         
        for(int i=0; i<list.size(); i++){
            //System.out.println(list.get(i));
            chars[i] = list.get(i);
        }
         
        return list.size();
    }
}


class Solution {
    public int compress(char[] chars) {
        List<Character> list = new ArrayList<>();
        int curr = 0;
        int count = 1;
         
        for(int i=0; i<chars.length; i++){
            int next = i + 1;
            if(next < chars.length && chars[i] == chars[next]){
                count++;
            }else{
                list.add(chars[curr]);
                curr = next < chars.length ? next : i;
                if(count > 1){
                    for(int j=0; j<(count+"").length(); j++){
                        list.add((count+"").charAt(j));
                    }
                }
                count = 1;
            }
        }
         
        for(int i=0; i<list.size(); i++){
            //System.out.println(list.get(i));
            chars[i] = list.get(i);
        }
         
        return list.size();
    }
}


class Solution {
    public int compress(char[] chars) {
        int index = 0;
        int buffer = 0;
        
        for (int i = 0; i < chars.length; i++) {
            int next = i + 1;
            if (next == chars.length || chars[i] != chars[next]) {
                chars[buffer++] = chars[index];
                if (index < i) {
                    String count = (i - index + 1) + "";
                    for(int j=0; j<count.length(); j++){
                        chars[buffer++] = count.charAt(j);
                    }
                }
                index = next;
            }
        }
        return buffer;
    }
}


[문제] https://leetcode.com/problems/string-compression/



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
77 Programmers hashmap secret hooni 2020.04.30 0
76 Programmers fib secret hooni 2020.04.30 0
75 Programmers team game secret hooni 2020.04.30 0
74 Leetcode 692. Top K Frequent Words hooni 2020.04.15 80
73 Leetcode 2. Add Two Numbers hooni 2020.04.14 83
72 Leetcode 731. My Calendar II hooni 2020.04.15 87
71 Leetcode 75. Sort Colors hooni 2020.04.14 88
70 Leetcode 347. Top K Frequent Elements hooni 2020.04.14 89
69 Leetcode 20. Valid Parentheses hooni 2020.04.25 89
68 Leetcode 430. Flatten a Multilevel Doubly Linked List file hooni 2020.04.06 91
67 Leetcode 23. Merge k Sorted Lists hooni 2020.04.14 91
66 Leetcode 729. My Calendar I hooni 2020.04.15 91
65 Leetcode 253. Meeting Rooms II hooni 2020.04.15 91
64 Leetcode 7. Reverse Integer hooni 2020.04.28 91
63 Leetcode 62. Unique Paths file hooni 2020.04.09 92
62 Leetcode 350. Intersection of Two Arrays II hooni 2020.04.28 92
61 Leetcode 852. Peak Index in a Mountain Array hooni 2020.04.28 92
60 Leetcode 946. Validate Stack Sequences hooni 2020.04.08 94
59 Leetcode 973. K Closest Points to Origin hooni 2020.04.15 94
» Leetcode 443. String Compression hooni 2020.04.25 94
Board Pagination Prev 1 2 3 4 Next
/ 4