DDSA Solutions

3163. String Compression III

Time: O(n)
Space: O(n)
Advertisement

Approach

Run-length encoding capping each run at 9; append count then character.

Key Techniques

String

String problems range from simple character counting to complex pattern matching. Common approaches include two pointers, sliding window, prefix hashing, and the KMP algorithm. In C#, strings are immutable — use StringBuilder for efficient concatenation inside loops.

3163.cs
C#
// Approach: Run-length encoding capping each run at 9; append count then character.
// Time: O(n) Space: O(n)

public class Solution
{
    public string CompressedString(string word)
    {
        int n = word.Length;
        StringBuilder sb = new StringBuilder();

        for (int i = 0, j = 0; i < n; i = j)
        {
            int count = 0;
            while (j < n && word[j] == word[i] && count < 9)
            {
                ++j;
                ++count;
            }
            sb.Append(count.ToString()).Append(word[i]);
        }

        return sb.ToString();
    }
}
Advertisement
Was this solution helpful?