Goal: Implement a string compression algorithm that efficiently groups sequences of identical characters.
There are two popular approaches to solving this problem:
Iterative Approach (for loop):
Using Regular Expressions:
/(.)\1*/g is perfect for this: (.) captures any character, and \1* looks for its repetitions.replace method with a callback function to format the result.const compressString = (str) => {
// If the string is empty, there's no point in continuing. Return an empty string.
if (!str) {
return '';
}
// `result` will store our compressed string.
let result = '';
// `count` tracks the number of repetitions for the current character.
let count = 1;
// Iterate through the string using a for loop.
for (let i = 0; i < str.length; i++) {
// Look ahead: compare the current character (str[i]) with the next one (str[i + 1]).
// If they match, the sequence continues.
if (str[i] === str[i + 1]) {
// Just increment the counter and move to the next iteration.
count++;
} else {
// If the next character is different or it's the end of the string,
// it means the sequence of the current character has been interrupted.
// Append the character itself to the result.
// Then check the counter: if it's greater than 1, append it as well.
// If the counter is 1, we add nothing, as per the requirements.
result += str[i] + (count > 1 ? count : '');
// Reset the counter to 1 for the next sequence of characters.
count = 1;
}
}
// Return the final compressed string.
return result;
};Solution Analysis:
Pros:
Cons:
result and count variables.const compressString = (str) => {
// Simple check for an empty string.
if (!str) {
return '';
}
// Use the `replace` method with a regular expression.
// The regular expression /(.)\1*/g finds all sequences
// of identical characters in the string.
// ( . ) - Captures any single character (except newline) into a group.
// \1* - Matches zero or more repetitions of what was captured in the first group.
// g - Global flag to find all matches, not just the first one.
return str.replace(/(.)\1*/g, (match, char) => {
// `replace` calls this callback function for each match found.
// `match` is the entire matched sequence (e.g., "AAAA").
// `char` is the character captured by the first group (e.g., "A").
// If the length of the matched sequence is greater than 1,
// return the character + its length.
// Otherwise (if the length is 1), return only the character itself.
return match.length > 1 ? char + match.length : char;
});
};Solution Analysis:
Pros:
Cons:
You need to create a function compressString that performs basic string compression using the Run-Length Encoding (RLE) algorithm.
Compression Rules:
// 'AAA' -> 'A3', 'B' -> 'B', 'CC' -> 'C2'
compressString('AAABCC'); // "A3BC2"
// No consecutive repetitions
compressString('ABC'); // "ABC"
// Long sequence
compressString('WWWWWWWWWWWW'); // "W12"
// Empty string
compressString(''); // ""compressString.'a' and 'A' are different characters).Goal: Implement a string compression algorithm that efficiently groups sequences of identical characters.
There are two popular approaches to solving this problem:
Iterative Approach (for loop):
Using Regular Expressions:
/(.)\1*/g is perfect for this: (.) captures any character, and \1* looks for its repetitions.replace method with a callback function to format the result.const compressString = (str) => {
// If the string is empty, there's no point in continuing. Return an empty string.
if (!str) {
return '';
}
// `result` will store our compressed string.
let result = '';
// `count` tracks the number of repetitions for the current character.
let count = 1;
// Iterate through the string using a for loop.
for (let i = 0; i < str.length; i++) {
// Look ahead: compare the current character (str[i]) with the next one (str[i + 1]).
// If they match, the sequence continues.
if (str[i] === str[i + 1]) {
// Just increment the counter and move to the next iteration.
count++;
} else {
// If the next character is different or it's the end of the string,
// it means the sequence of the current character has been interrupted.
// Append the character itself to the result.
// Then check the counter: if it's greater than 1, append it as well.
// If the counter is 1, we add nothing, as per the requirements.
result += str[i] + (count > 1 ? count : '');
// Reset the counter to 1 for the next sequence of characters.
count = 1;
}
}
// Return the final compressed string.
return result;
};Solution Analysis:
Pros:
Cons:
result and count variables.const compressString = (str) => {
// Simple check for an empty string.
if (!str) {
return '';
}
// Use the `replace` method with a regular expression.
// The regular expression /(.)\1*/g finds all sequences
// of identical characters in the string.
// ( . ) - Captures any single character (except newline) into a group.
// \1* - Matches zero or more repetitions of what was captured in the first group.
// g - Global flag to find all matches, not just the first one.
return str.replace(/(.)\1*/g, (match, char) => {
// `replace` calls this callback function for each match found.
// `match` is the entire matched sequence (e.g., "AAAA").
// `char` is the character captured by the first group (e.g., "A").
// If the length of the matched sequence is greater than 1,
// return the character + its length.
// Otherwise (if the length is 1), return only the character itself.
return match.length > 1 ? char + match.length : char;
});
};Solution Analysis:
Pros:
Cons:
You need to create a function compressString that performs basic string compression using the Run-Length Encoding (RLE) algorithm.
Compression Rules:
// 'AAA' -> 'A3', 'B' -> 'B', 'CC' -> 'C2'
compressString('AAABCC'); // "A3BC2"
// No consecutive repetitions
compressString('ABC'); // "ABC"
// Long sequence
compressString('WWWWWWWWWWWW'); // "W12"
// Empty string
compressString(''); // ""compressString.'a' and 'A' are different characters).The code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!