반응형

https://leetcode.com/problems/string-compression/description/

 

String Compression - LeetCode

Can you solve this real interview question? String Compression - Given an array of characters chars, compress it using the following algorithm: Begin with an empty string s. For each group of consecutive repeating characters in chars: * If the group's leng

leetcode.com

투포인터를 두고 풀었습니다.

하나의 포인터는 chars를 순차적으로 탐색하면서 연속된 동일 문자 개수를 구해주고,

또 다른 포인터는 새로운 문자가 나오면 chars에 압축 내역을 반영해주었습니다.

class Solution {
public:
    int compress(vector<char>& chars) {
        chars.push_back('?'); // dummy

        char ch = chars[0];
        int cnt = 1;
        int j = 0;
        for(int i=1; i<chars.size(); i++) {
            if(chars[i] != ch) {
                chars[j++]= ch;
                if(cnt >= 10) {
                    string cntStr = to_string(cnt);
                    for(char cntCh : cntStr) {
                        chars[j++] = cntCh;
                    }
                } else if(cnt > 1) {
                    chars[j++] = cnt + '0';
                }
                cnt = 0;
                ch = chars[i];
            }
            cnt++;
        }

        return j;
    }
};
반응형

+ Recent posts