Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode String Compression problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode String Compression problem solution, we have 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:

  1. If the group’s length is 1, append the character to s.
  2. Otherwise, append the character followed by the group’s length.

The compressed string s should not be returned separately, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars.

After you are done modifying the input array, return the new length of the array. You must write an algorithm that uses only constant extra space.

Leetcode String Compression problem solution

Leetcode String Compression problem solution in Python.

class Solution:
    def compress(self, array: List[str]) -> int:
        ans=[]
        i=0
        while i < len(array):
            temp=array[i]
            count=i+1
            while count<len(array)and array[count]==temp:
                count+=1
                if count==len(array):
                    break
            ans.append(temp)
            if count-i>1 and count-i<10:
                ans.append(str(count-i))
            if count-i>9:
                num=count-i
                dup=[]
                while num:
                    dup.append(str(num%10))
                    num//=10
                for j in range(len(dup)-1,-1,-1):
                    ans.append(dup[j])
            i=count
        array.clear()
        for i in range(len(ans)):
            array.append(ans[i])
        return len(ans)

String Compression problem solution in Java.

class Solution {
    public int compress(char[] chars) {
     if (chars.length == 1) return chars.length;
    int index = 0, newChar_start_at = 0,start = 0;
    while (start< chars.length) {
        int count = 0;
        char val = chars[newChar_start_at];
        for (;start < chars.length && chars[start] == val;count++,start++); 
        chars[index] = val;
        index++;
        if (count > 1) {
            for (char c : Integer.toString(count).toCharArray()) {
                chars[index] = c;
                index++;
            }
        }
        newChar_start_at = start;
    }
    return index;
    }
}

Problem solution in C++.

class Solution {
public:
    int compress(vector<char>& chars) {
        int n=(int)chars.size(), write_idx=0, cnt=1;
        if (n<=1) return n;
        for (int i=1; i<n; ++i){
            if (chars[i-1]==chars[i]){
                ++cnt;
            } else {
                write(chars, write_idx, chars[i-1], cnt);
                cnt=1;
            }
        }
        write(chars, write_idx, chars.back(), cnt);
        chars.resize(write_idx);
        return write_idx;
    }

    void write(vector<char>& chars, int& write_idx, char c, int cnt){
        chars[write_idx++]=c;
        if (cnt==1) return;
        string s=to_string(cnt);
        for (int i=0; i<s.size(); ++i){
            chars[write_idx++]=s[i];
        }
    }
};

Problem solution in C.

int compress(char* chars, int charsSize){

int char_count = 1;
int cnt = 0;
int count_index = 1; 
char test = chars[0];
char count_buf[20]; 

for(int i = 1;i<charsSize;i++)
{
    if(chars[i]==test)
    {
        char_count++;
    }
    else
    {
        if(char_count>1)
        {
            cnt = sprintf(count_buf,"%d",char_count); 
            memcpy(chars+count_index, count_buf, cnt);
            
            count_index = count_index + cnt;
            char_count = 1;
        }
       
        test = chars[i];
        chars[count_index] = test;
        count_index++;    
    }
}

if(char_count > 1)
{
    cnt = sprintf(count_buf,"%d",char_count);
    memcpy(chars+count_index, count_buf, cnt);
    count_index = count_index + cnt;
}

return count_index;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes