Given an array of characters chars, compress it using the following algorithm:
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.
To solve this problem, we need to iterate through the array while keeping track of the current character and its count. When we encounter a new character, we append the current character and its count (if greater than 1) to the array. We need to ensure that we do this in place to meet the space complexity requirement.
The brute force solution involves creating a new array to store the compressed version of the input array. This is not space efficient but helps us understand the steps involved.
function compressBruteForce(chars: string[]): number { const n = chars.length; let compressed: string[] = []; let i = 0; while (i < n) { let char = chars[i]; let count = 0; while (i < n && chars[i] === char) { i++; count++; } compressed.push(char); if (count > 1) { compressed.push(...count.toString().split('')); } } for (let j = 0; j < compressed.length; j++) { chars[j] = compressed[j]; } return compressed.length; }
The brute force solution is not space efficient and does not meet the constraint of using only constant extra space.
The optimized solution involves modifying the input array in place to store the compressed version. We use two pointers: one for reading the input array and one for writing the compressed output.
function compress(chars: string[]): number { let writeIndex = 0; let i = 0; while (i < chars.length) { let char = chars[i]; let count = 0; // Count the number of consecutive repeating characters while (i < chars.length && chars[i] === char) { i++; count++; } // Write the character chars[writeIndex] = char; writeIndex++; // Write the count if greater than 1 if (count > 1) { let countStr = count.toString(); for (let j = 0; j < countStr.length; j++) { chars[writeIndex] = countStr[j]; writeIndex++; } } } return writeIndex; } <h3> Time Complexity Analysis: </h3> <ul> <li> <strong>Time Complexity:</strong> O(n), where n is the length of the array. We iterate through the array once to count characters and once to write the result.</li> <li> <strong>Space Complexity:</strong> O(1), as we use only a constant amount of extra space for variables.</li> </ul> <h3> Improvements Over Basic Solution: </h3> <ul> <li>The optimized solution reduces space complexity to O(1) by modifying the input array in place.</li> </ul> <h2> Edge Cases and Testing: </h2> <h3> Edge Cases: </h3> <ol> <li>The array contains only one character.</li> <li>The array contains no consecutive repeating characters.</li> <li>The array contains a large number of consecutive repeating characters.</li> </ol> <h3> Test Cases: </h3> <pre class="brush:php;toolbar:false">console.log(compressBruteForce(["a","a","b","b","c","c","c"])); // 6, ["a","2","b","2","c","3"] console.log(compressBruteForce(["a"])); // 1, ["a"] console.log(compressBruteForce(["a","b","b","b","b","b","b","b","b","b","b","b","b"])); // 4, ["a","b","1","2"] console.log(compressBruteForce(["a","a","a","a","a","a","a","a","a","a"])); // 3, ["a","1","0"] console.log(compressBruteForce(["a","b","c"])); // 3, ["a","b","c"] console.log(compress(["a","a","b","b","c","c","c"])); // 6, ["a","2","b","2","c","3"] console.log(compress(["a"])); // 1, ["a"] console.log(compress(["a","b","b","b","b","b","b","b","b","b","b","b","b"])); // 4, ["a","b","1","2"] console.log(compress(["a","a","a","a","a","a","a","a","a","a"])); // 3, ["a","1","0"] console.log(compress(["a","b","c"])); // 3, ["a","b","c"]
String Manipulation:
In-Place Algorithms:
By practicing such problems and strategies, you can improve your problem-solving skills and be better prepared for various coding challenges.
The above is the detailed content of Typescript Coding Chronicles: String Compression. For more information, please follow other related articles on the PHP Chinese website!