Encoding string to reduce its size in JavaScript

Problem

We need to write a JavaScript function that takes a string and encodes it using a compression format. The function should compare the encoded string with the original and return whichever is smaller.

The encoding rule is:

  • n[s], where s inside the square brackets is being repeated exactly n times.

For example, "ddd" can be encoded to "3[d]", but since "3[d]" has 4 characters while "ddd" has only 3, the function should return "ddd".

Example Input and Output

For the input string:

'aabcaabcd'

The expected output is:

'2[aabc]d'

Solution

The solution uses dynamic programming to find the optimal encoding by testing different substring combinations and checking if patterns can be compressed:

const str = 'aabcaabcd';

function encode(s) {
    const { length } = s;
    const dp = Array(length).fill([]);
    
    dp.forEach((el, ind) => {
        dp[ind] = Array(length).fill(null);
    });
    
    for(let l = 1; l 

Output

2[aabc]d

How It Works

The algorithm works by:

  • Creating a 2D DP array where dp[i][j] represents the shortest encoding for substring from index i to j

  • For each substring, it tries splitting it at different positions and combining the optimal encodings

  • It checks if the substring contains repeating patterns by duplicating it and finding the first occurrence of the original pattern

  • If a pattern repeats, it creates an encoded format like n[pattern] and compares its length with the original

Key Points

  • The function only returns the encoded version if it's actually shorter than the original

  • It uses dynamic programming to avoid recomputing optimal encodings for the same substrings

  • The algorithm has a time complexity of O(n³) where n is the string length

Conclusion

This encoding function effectively compresses strings by identifying repeating patterns and representing them in a compact n[pattern] format. It ensures optimal compression by comparing all possible encodings and returning the shortest representation.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements