🌐
LeetCode
leetcode.com › problems › string-compression
String Compression - LeetCode
String Compression · Medium · 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: If the group's length is 1, append the ...
🌐
AlgoMonster
algo.monster › home › 443. string compression
443. String Compression - In-Depth Explanation
1class Solution { 2 public int compress(char[] chars) { 3 // writeIndex: position to write the compressed result 4 // arrayLength: total length of the input array 5 int writeIndex = 0; 6 int arrayLength = chars.length; 7 8 // Use two pointers to traverse the array 9 // currentIndex: start of current group of same characters 10 // nextIndex: points to the next different character 11 int currentIndex = 0; 12 13 while (currentIndex < arrayLength) { 14 // Find the end of current group of identical characters 15 int nextIndex = currentIndex + 1; 16 while (nextIndex < arrayLength && chars[nextIndex]
🌐
Medium
medium.com › repeat-code-with-leetcode › repeat-code-with-leetcode-string-compression-88f854e56a56
Repeat Code with Leetcode: String Compression | by Curt Corginia | Repeat Code With LeetCode | Medium
December 26, 2024 - Basically, the problem is similar to its counterpart, only you have to do it in place, you do not include the count if the count is 1, a count with multiple digits like “13” has to be separated by digits, you have to return the size, and…well…frankly, I suspect that 3000 people found the description confusing as hell. If I were the ruler of Leetcode, I might have added the following: Clarification 1: The count you return represents the length of your new string.
🌐
Medium
medium.com › @hamnaqaseem › leetcode-443-string-compression-python-solution-77b28c762e34
LeetCode # 443: String Compression — Python Solution | by Hamna Qaseem | Medium
July 19, 2023 - Step 12: Finally, return the value of i. This value represents the new length of the modified character array, which now contains the compressed characters. The time complexity is O(n), where n is length of chars. Because we are iterating through an array at once. The space complexity is O(1), as we are using constant extra space. ... class Solution: def compress(self, chars: List[str]) -> int: i = 0 counter = 1 for j in range(1, len(chars) + 1): if j < len(chars) and chars[j] == chars[j-1]: counter += 1 else: chars[i] = chars[j-1] i = i + 1 if counter > 1: for k in str(counter): chars[i] = k i += 1 counter = 1 return i
🌐
WalkCCC
walkccc.me › LeetCode › problems › 443
443. String Compression - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
🌐
AlgoMonster
algo.monster › home › 1531. string compression ii
1531. String Compression II - In-Depth Explanation
Solution: Always validate input constraints and handle edge cases: ... def get_compression_length(frequency): if frequency == 1: return 1 if frequency < 10: return 2 if frequency < 100: return 3 if frequency == 100: # Explicitly handle the upper bound return 4 # Handle unexpected cases return ...
🌐
LeetCode
leetcode.com › problems › string-compression-ii
String Compression II - LeetCode
Given a string s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length. Find the minimum length of the run-length encoded version of s after deleting at most k characters.
🌐
LeetCode
leetcode.com › problems › string-compression-iii
String Compression III - LeetCode
String Compression III · Medium ... is not empty, use the following operation: Remove a maximum length prefix of word made of a single character c repeating at most 9 times....
🌐
NeetCode
neetcode.io › problems › string compression
String Compression - NeetCode
Begin with an empty string s. For each group of consecutive repeating characters in chars: If the group's length is 1, append the character to s. Otherwise, append the character followed by the group's length. The compressed string s should not be returned separately, but instead, be stored ...
Find elsewhere
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0400-0499 › 0443.String Compression › README_EN.md
leetcode/solution/0400-0499/0443.String Compression/README_EN.md at main · doocs/leetcode · GitHub
class Solution { public int compress(char[] chars) { int k = 0, n = chars.length; for (int i = 0, j = i + 1; i < n;) { while (j < n && chars[j] == chars[i]) { ++j; } chars[k++] = chars[i]; if (j - i > 1) { String cnt = String.valueOf(j - i); for (char c : cnt.toCharArray()) { chars[k++] = c; } } i = j; } return k; } }
Author: doocs
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 3100-3199 › 3163.String Compression III › README_EN.md
leetcode/solution/3100-3199/3163.String Compression III/README_EN.md at main · doocs/leetcode · GitHub
class Solution: def compressedString(self, word: str) -> str: g = groupby(word) ans = [] for c, v in g: k = len(list(v)) while k: x = min(9, k) ans.append(str(x) + c) k -= x return "".join(ans) class Solution { public String compressedString(String word) { StringBuilder ans = new StringBuilder(); int n = word.length(); for (int i = 0; i < n;) { int j = i + 1; while (j < n && word.charAt(j) == word.charAt(i)) { ++j; } int k = j - i; while (k > 0) { int x = Math.min(9, k); ans.append(x).append(word.charAt(i)); k -= x; } i = j; } return ans.toString(); } }
Author: doocs
🌐
Medium
medium.com › @miniChang8 › leetcode-string-compression-fb8f5bd9b84c
Leetcode — String Compression
April 1, 2022 - Leetcode: https://leetcode.com/problems/string-compression/ Problem: 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: If ...
🌐
LeetCode
leetcode.com › discuss › interview-question › 719930 › an-odd-question-about-string-compression
An Odd Question about String Compression - Discuss - LeetCode
To obtain the compressed representation, we replace each segment of equal characters in the string with the number of characters in the segment followed by the character(e.x we replace segment "CCCC" with 4C).
🌐
Blogger
massivealgorithms.blogspot.com › 2019 › 04 › leetcode-443-string-compression.html
Massive Algorithms: LeetCode 443 - String Compression
public int compress(char[] chars) { int anchor = 0, write = 0; for (int read = 0; read < chars.length; read++) { if (read + 1 == chars.length || chars[read + 1] != chars[read]) { chars[write++] = chars[anchor]; if (read > anchor) { for (char c: ("" + (read - anchor + 1)).toCharArray()) { chars[write++] = c; } } anchor = read + 1; } } return write; } https://leetcode.com/problems/string-compression/discuss/92559/Simple-Easy-to-Understand-Java-solution ·
🌐
InterviewSkool
interviewskool.com › home › practice › dsa problems › string compression
String Compression: LeetCode #443 Solution & Approach Guide | InterviewSkool
September 23, 2026 - Compress it in place using this rule: look at each group of consecutive equal characters. If a group has only one character, write just that character. If a group has more than one character, write the character followed by the group's size ...
🌐
Favtutor
favtutor.com › articles › string-compression
String Compression Problem (C++, Java, Python)
January 30, 2024 - The simplest way to solve the String Compression problem is by using two pointers. One pointer for iterating through the original character array and one for keeping track of the current position in the compressed array.
🌐
edSlash
edslash.com › home › leetcode challenge #443. string compression
LeetCode Challenge #443. String Compression - edSlash
December 14, 2023 - Begin with an empty string s. For each group of consecutive repeating characters in chars: If the group’s length is 1, append the character to s. 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.
🌐
LeetCode
leetcode.ca › all › 1531.html
1531. String Compression II - Leetcode Solutions
Given a string s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length. Find the minimum length of the run-length encoded version of s after deleting at most k characters. ... Input: s = "aaabcccd", k = 2 Output: 4 ...