101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 2 main approaches to solve this problem:

  1. Two-Pointer Approach - Complex approach
  2. Two-Pass Approach - Complex approach

Approach 1: Two-Pointer Approach

We can use a two-pointer approach to iterate through the string and count consecutive characters. We'll maintain a pointer to the current character and another to track the count of consecutive occurrences.

Input: "aabcccccaaa"

a
a
b
c
c
c
c
c
a
a
a

Count 'a': 2, Add "a2" to result

a
a
b
c
c
c
c
c
a
a
a

Count 'b': 1, Add "b" to result

a
a
b
c
c
c
c
c
a
a
a

Count 'c': 5, Add "c5" to result

a
a
b
c
c
c
c
c
a
a
a

Count 'a': 3, Add "a3" to result

Final result: "a2bc5a3"

Algorithm:

  1. Initialize an empty result string.
  2. Initialize a counter to 1 (to count consecutive characters).
  3. Iterate through the string from the second character to the end.
  4. If the current character is the same as the previous one, increment the counter.
  5. If the current character is different from the previous one, append the previous character to the result.
  6. If the counter is greater than 1, also append the counter to the result.
  7. Reset the counter to 1 and continue.
  8. After the loop, handle the last character and its count.
  9. Compare the length of the compressed string with the original string and return the shorter one.

Time Complexity:

O(n)

where n is the length of the input string. We need to iterate through each character once.

Space Complexity:

O(n)

In the worst case, the compressed string could be as long as the original string (if there are no repeated characters).

Approach 2: Two-Pass Approach

To optimize the solution, we can first calculate the length of the compressed string without actually creating it. If the compressed length is not smaller than the original length, we can return the original string immediately. Otherwise, we perform the compression in a second pass.

Algorithm:

  1. First pass: Calculate the length of the compressed string.
  2. Initialize a counter to 1 and a compressed length to 0.
  3. Iterate through the string and count consecutive characters.
  4. For each group of consecutive characters, add 1 to the compressed length (for the character itself).
  5. If the count is greater than 1, add the number of digits in the count to the compressed length.
  6. If the compressed length is not smaller than the original length, return the original string.
  7. Second pass: Create the compressed string using the same logic as the first approach.
  8. Return the compressed string.

Time Complexity:

O(n)

where n is the length of the input string. We need to iterate through each character once in each pass, so it's still O(n).

Space Complexity:

O(n)

In the worst case, the compressed string could be as long as the original string.

Pseudocode

solution.pseudo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function compress(s):
if s is empty:
return s
result = ""
count = 1
for i from 1 to s.length - 1:
if s[i] == s[i-1]:
count++
else:
result += s[i-1]
if count > 1:
result += count.toString()
count = 1
// Handle the last character
result += s[s.length - 1]
if count > 1:
result += count.toString()
// Return the shorter string
if result.length < s.length:
return result
else:
return s
ProblemSolutionCode
101 Logo
onenoughtone