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:
1, append the character to s.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.
The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
The only group is "a", which remains uncompressed since it's a single character.
The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
1 <= chars.length <= 2000chars[i] is a lowercase English letter, uppercase English letter, digit, or symbol.How do you know if you are at the end of a consecutive group of characters?
No developer has claimed the Python throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
We use two pointers (a read pointer fast and a write pointer slow) and a counter count to achieve in-place compression.
Initialization:
chars. This ensures that the last group of consecutive characters can also be correctly processed within the loop. Java and C# is a little different because array is fixed size.slow = 0: The slow pointer points to the starting character of the current write segment, and it also represents the length of the final compressed array.count = 1: Records the number of occurrences of the current consecutive character chars[slow].Traversal and Compression:
fast pointer starts traversing the array chars from index 1 (until the sentinel character).chars[fast] is equal to chars[slow])
count.chars[fast] is not equal to chars[slow])
Return Result:
fast pointer has traversed the entire array (including the sentinel), the value of the slow pointer is the new length of the compressed array.class Solution:
def compress(self, chars: List[str]) -> int:
chars.append(" ") # Append an extra special char to process the last char easier
slow = 0 # Slow pointer. This is the answer.
count = 1 # Count of consecutive repeating characters
for fast in range(1, len(chars)):
if chars[fast] == chars[slow]:
count += 1
continue # 'continue' is better than 'else' because 'else' will introduce more indents
if count == 1:
slow += 1
# Don't need to append the 'count' when count is 1.
chars[slow] = chars[fast]
continue # 'continue' is better than 'else' because 'else' will introduce more indents
# Append the 'count'
for c in str(count):
slow += 1
chars[slow] = c
slow += 1
chars[slow] = chars[fast]
count = 1
return slow
No developer has claimed the Java throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
We use two pointers (a read pointer fast and a write pointer slow) and a counter count to achieve in-place compression.
Initialization:
chars. This ensures that the last group of consecutive characters can also be correctly processed within the loop. Java and C# is a little different because array is fixed size.slow = 0: The slow pointer points to the starting character of the current write segment, and it also represents the length of the final compressed array.count = 1: Records the number of occurrences of the current consecutive character chars[slow].Traversal and Compression:
fast pointer starts traversing the array chars from index 1 (until the sentinel character).chars[fast] is equal to chars[slow])
count.chars[fast] is not equal to chars[slow])
Return Result:
fast pointer has traversed the entire array (including the sentinel), the value of the slow pointer is the new length of the compressed array.import java.util.ArrayList;
import java.util.List;
class Solution {
public int compress(char[] chars) {
int slow = 0; // Slow pointer. This is the answer.
int count = 1; // Count of consecutive repeating characters
for (int fast = 1; fast <= chars.length; fast++) { // it is "<=", not "<"
var charFast = (fast == chars.length ? ' ' : chars[fast]);
if (charFast == chars[slow]) {
count++;
continue; // 'continue' is better than 'else' because 'else' will introduce more indents
}
if (count == 1) {
slow++;
// Don't need to append the 'count' when count is 1.
if (slow < chars.length) {
chars[slow] = charFast;
}
continue; // 'continue' is better than 'else' because 'else' will introduce more indents
}
// Append the 'count'
for (char c : String.valueOf(count).toCharArray()) {
slow++;
chars[slow] = c;
}
slow++;
if (slow < chars.length) {
chars[slow] = charFast;
}
count = 1;
}
return slow;
}
}
No developer has claimed the C++ throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
We use two pointers (a read pointer fast and a write pointer slow) and a counter count to achieve in-place compression.
Initialization:
chars. This ensures that the last group of consecutive characters can also be correctly processed within the loop. Java and C# is a little different because array is fixed size.slow = 0: The slow pointer points to the starting character of the current write segment, and it also represents the length of the final compressed array.count = 1: Records the number of occurrences of the current consecutive character chars[slow].Traversal and Compression:
fast pointer starts traversing the array chars from index 1 (until the sentinel character).chars[fast] is equal to chars[slow])
count.chars[fast] is not equal to chars[slow])
Return Result:
fast pointer has traversed the entire array (including the sentinel), the value of the slow pointer is the new length of the compressed array.class Solution {
public:
int compress(vector<char>& chars) {
chars.push_back(' '); // Append an extra special char to process the last char easier
int slow = 0; // Slow pointer. This is the answer.
int count = 1; // Count of consecutive repeating characters
for (int fast = 1; fast < chars.size(); ++fast) {
if (chars[fast] == chars[slow]) {
count++;
continue; // 'continue' is better than 'else' because 'else' will introduce more indents
}
if (count == 1) {
slow++;
// Don't need to append the 'count' when count is 1.
chars[slow] = chars[fast];
continue; // 'continue' is better than 'else' because 'else' will introduce more indents
}
// Append the 'count'
for (char c : to_string(count)) {
slow++;
chars[slow] = c;
}
slow++;
chars[slow] = chars[fast];
count = 1;
}
return slow;
}
};
No developer has claimed the C# throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
We use two pointers (a read pointer fast and a write pointer slow) and a counter count to achieve in-place compression.
Initialization:
chars. This ensures that the last group of consecutive characters can also be correctly processed within the loop. Java and C# is a little different because array is fixed size.slow = 0: The slow pointer points to the starting character of the current write segment, and it also represents the length of the final compressed array.count = 1: Records the number of occurrences of the current consecutive character chars[slow].Traversal and Compression:
fast pointer starts traversing the array chars from index 1 (until the sentinel character).chars[fast] is equal to chars[slow])
count.chars[fast] is not equal to chars[slow])
Return Result:
fast pointer has traversed the entire array (including the sentinel), the value of the slow pointer is the new length of the compressed array.public class Solution
{
public int Compress(char[] chars)
{
int slow = 0; // Slow pointer. This is the answer.
int count = 1; // Count of consecutive repeating characters
for (int fast = 1; fast <= chars.Length; fast++)
{ // it is "<=", not "<"
char charFast = (fast == chars.Length ? ' ' : chars[fast]);
if (charFast == chars[slow])
{
count++;
continue; // 'continue' is better than 'else' because 'else' will introduce more indents
}
if (count == 1)
{
slow++;
// Don't need to append the 'count' when count is 1.
if (slow < chars.Length)
{
chars[slow] = charFast;
}
continue; // 'continue' is better than 'else' because 'else' will introduce more indents
}
// Append the 'count'
foreach (char c in count.ToString())
{
slow++;
chars[slow] = c;
}
slow++;
if (slow < chars.Length)
{
chars[slow] = charFast;
}
count = 1;
}
return slow;
}
}
No developer has claimed the Javascript throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
We use two pointers (a read pointer fast and a write pointer slow) and a counter count to achieve in-place compression.
Initialization:
chars. This ensures that the last group of consecutive characters can also be correctly processed within the loop. Java and C# is a little different because array is fixed size.slow = 0: The slow pointer points to the starting character of the current write segment, and it also represents the length of the final compressed array.count = 1: Records the number of occurrences of the current consecutive character chars[slow].Traversal and Compression:
fast pointer starts traversing the array chars from index 1 (until the sentinel character).chars[fast] is equal to chars[slow])
count.chars[fast] is not equal to chars[slow])
Return Result:
fast pointer has traversed the entire array (including the sentinel), the value of the slow pointer is the new length of the compressed array./**
* @param {character[]} chars
* @return {number}
*/
var compress = function(chars) {
chars.push(' ') // Append an extra special char to process the last char easier
let slow = 0 // Slow pointer. This is the answer.
let count = 1 // Count of consecutive repeating characters
for (let fast = 1; fast < chars.length; fast++) {
if (chars[fast] === chars[slow]) {
count++
continue // 'continue' is better than 'else' because 'else' will introduce more indents
}
if (count === 1) {
slow++
// Don't need to append the 'count' when count is 1.
chars[slow] = chars[fast]
continue // 'continue' is better than 'else' because 'else' will introduce more indents
}
// Append the 'count'
for (const c of count.toString()) {
slow++
chars[slow] = c
}
slow++
chars[slow] = chars[fast]
count = 1
}
return slow
}
No developer has claimed the Go throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
We use two pointers (a read pointer fast and a write pointer slow) and a counter count to achieve in-place compression.
Initialization:
chars. This ensures that the last group of consecutive characters can also be correctly processed within the loop. Java and C# is a little different because array is fixed size.slow = 0: The slow pointer points to the starting character of the current write segment, and it also represents the length of the final compressed array.count = 1: Records the number of occurrences of the current consecutive character chars[slow].Traversal and Compression:
fast pointer starts traversing the array chars from index 1 (until the sentinel character).chars[fast] is equal to chars[slow])
count.chars[fast] is not equal to chars[slow])
Return Result:
fast pointer has traversed the entire array (including the sentinel), the value of the slow pointer is the new length of the compressed array.// A test cannot pass. Reason is still unknown
func compress(chars []byte) int {
chars = append(chars, ' ') // Append an extra special char to process the last char easier
slow := 0 // Slow pointer. This is the answer.
count := 1 // Count of consecutive repeating characters
for fast := 1; fast < len(chars); fast++ {
if chars[fast] == chars[slow] {
count++
continue // 'continue' is better than 'else' because 'else' will introduce more indents
}
if count == 1 {
slow++
// Don't need to append the 'count' when count is 1.
chars[slow] = chars[fast]
continue // 'continue' is better than 'else' because 'else' will introduce more indents
}
// Append the 'count'
for _, c := range strconv.Itoa(count) {
slow++
chars[slow] = byte(c)
}
slow++
chars[slow] = chars[fast]
count = 1
}
return slow
}
No developer has claimed the Ruby throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
We use two pointers (a read pointer fast and a write pointer slow) and a counter count to achieve in-place compression.
Initialization:
chars. This ensures that the last group of consecutive characters can also be correctly processed within the loop. Java and C# is a little different because array is fixed size.slow = 0: The slow pointer points to the starting character of the current write segment, and it also represents the length of the final compressed array.count = 1: Records the number of occurrences of the current consecutive character chars[slow].Traversal and Compression:
fast pointer starts traversing the array chars from index 1 (until the sentinel character).chars[fast] is equal to chars[slow])
count.chars[fast] is not equal to chars[slow])
Return Result:
fast pointer has traversed the entire array (including the sentinel), the value of the slow pointer is the new length of the compressed array.# @param {Character[]} chars
# @return {Integer}
def compress(chars)
chars << " " # Append an extra special char to process the last char easier
slow = 0 # Slow pointer. This is the answer.
count = 1 # Count of consecutive repeating characters
(1...chars.length).each do |fast|
if chars[fast] == chars[slow]
count += 1
next # 'next' is better than 'else' because 'else' will introduce more indents
end
if count == 1
slow += 1
# Don't need to append the 'count' when count is 1.
chars[slow] = chars[fast]
next # 'next' is better than 'else' because 'else' will introduce more indents
end
# Append the 'count'
count.to_s.each_char do |c|
slow += 1
chars[slow] = c
end
slow += 1
chars[slow] = chars[fast]
count = 1
end
slow
end
No developer has claimed the Rust throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #443 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.